<!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>Document</title>
<style>
div.box{
width: 100px;
height: 100px;
border: 1px solid red;
background-color: yellow;
position: relative;
transition: left 0.1s linear;
}
</style>
</head>
<body>
<div class="box">Box</div>
<script>
function moveBox(selector, callback){
var box = document.querySelector(selector);
var x = box.offsetLeft;
var step = 10;
var interval = setInterval(function(){
x += step;
/*
if(x >= 500 || x <= 0){
//x = 500;
//clearInterval(interval);
step *= -1;
}
*/
if(x >= 500){
x = 500;
clearInterval(interval);
callback(box);
}
box.style.left = x + "px";
}, 100);
}
// -------------------------------------------------------------------
moveBox("div.box", function(target) {
target.style.backgroundColor = "Red";
target.innerHTML = "완료";
});
</script>
</body>
</html>
'2022 SW전문인재양성사업 > 웹 프로그래밍 기본' 카테고리의 다른 글
Javascript (6) - 드래그 앤 드롭 실습2 (0) | 2022.07.27 |
---|---|
Javascript (5) - 드래그 앤 드롭 실습1 (0) | 2022.07.27 |
Javascript (3) - 드래그 앤 드롭으로 박스 옮기기 (0) | 2022.07.27 |
Javascript (2) - 함수 호출 관련 (0) | 2022.07.27 |
Javascript (1) - 기초 (0) | 2022.07.27 |