<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>자바스크립트 연습</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="box-container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<script src='app.js'></script>
</body>
</html>
body{
margin: 0;
padding: 0;
text-align: center;
}
#box-container{
width: 100%;
height: 100vh;
background-color: lightsalmon;
display: flex;
justify-content: center;
align-items: center;
}
.box{
width: 30px;
height: 30px;
border-radius: 50%;
background-color: lightpink;
transition: all 0.5s;
margin-left: 20px;
user-select: none;
}
const boxes = document.querySelectorAll('.box')
let index = 0, prevIndex = 0
let flag = true
function changePosition(){
console.log(index)
boxes[prevIndex].style.marginTop = '0px'
boxes[index].style.marginTop = '-30px'
if(index >= 4 || index <= 0){
flag = !flag
}
prevIndex = index
if(!flag) index++
else index--
}
setInterval(changePosition, 200)