<!DOCTYPE html>
<html lang="ko">
<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>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
<!-- Popper JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
<style>
div.box {
width: 400px;
height: 300px;
margin: 10px;
background-color: skyblue;
float: left;
position: relative;
}
#item1 {
width: 100px;
height: 40px;
background-color: red;
position: absolute;
left: 0;
top: 40px;
z-index: 1;
}
#item2 {
width: 100px;
height: 40px;
background-color: yellow;
position: absolute;
left: 0;
top: 90px;
z-index: 1;
}
#item3 {
width: 100px;
height: 40px;
background-color: green;
position: absolute;
left: 0;
top: 140px;
z-index: 1;
}
#colorBox{
background-color: yellow;
text-align: center;
width: 100px;
height: 100px;
top: 400px;
position: absolute;
transition: left 0.1s linear;
}
</style>
</head>
<body>
<div class="box">Box 1
<div id="item1">Drag item1</div>
<div id="item2">Drag item2</div>
<div id="item3">Drag item3</div>
</div>
<div class="box">Box 2</div>
<div>
<div class="box" id="colorBox">Color Box</div>
</div>
<script>
var box1 = document.querySelector('#item1');
var box2 = document.querySelector('#item2');
var box3 = document.querySelector('#item3');
box1.addEventListener('mousedown', function(e){
var gapX = e.clientX - box1.offsetLeft;
var gapY = e.clientY - box1.offsetTop;
function moveHandler(e2){
box1.style.left = e2.clientX - gapX + 'px';
box1.style.top = e2.clientY - gapY + 'px';
}
window.addEventListener('mousemove', moveHandler)
box1.addEventListener('mouseup', function(){
window.removeEventListener('mousemove', moveHandler)
});
});
box2.addEventListener('mousedown', function(e){
var gapX = e.clientX - box2.offsetLeft;
var gapY = e.clientY - box2.offsetTop;
function moveHandler(e2){
box2.style.left = e2.clientX - gapX + 'px';
box2.style.top = e2.clientY - gapY + 'px';
}
window.addEventListener('mousemove', moveHandler)
box2.addEventListener('mouseup', function(){
window.removeEventListener('mousemove', moveHandler)
});
});
box3.addEventListener('mousedown', function(e){
var gapX = e.clientX - box3.offsetLeft;
var gapY = e.clientY - box3.offsetTop;
function moveHandler(e2){
box3.style.left = e2.clientX - gapX + 'px';
box3.style.top = e2.clientY - gapY + 'px';
}
window.addEventListener('mousemove', moveHandler)
box3.addEventListener('mouseup', function(){
window.removeEventListener('mousemove', moveHandler)
});
});
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){
step *= -1;
box.style.backgroundColor = "Red"
}
if(x <= 0){
step *= -1;
box.style.backgroundColor = "Yellow"
}
box.style.left = x + "px";
}, 100);
}
// -------------------------------------------------------------------
moveBox("#colorBox")
</script>
</body>
</html>
'2022 SW전문인재양성사업 > 웹 프로그래밍 기본' 카테고리의 다른 글
Javascript (7) - 캐러셀 이미지 슬라이더 실습 (0) | 2022.07.27 |
---|---|
Javascript (6) - 드래그 앤 드롭 실습2 (0) | 2022.07.27 |
Javascript (4) - 박스 이동 함수 (0) | 2022.07.27 |
Javascript (3) - 드래그 앤 드롭으로 박스 옮기기 (0) | 2022.07.27 |
Javascript (2) - 함수 호출 관련 (0) | 2022.07.27 |