1、把方块拖拽进方格中
<style>
*{
margin: 0;
padding: 0;
}
.box1{
position: absolute;
width: 150px;
height: auto;
border: 1px solid ;
padding-bottom: 10px;
}
.box2{
position: absolute;
left: 300px;
width: 150px;
height: auto;
border: 1px solid ;
padding-bottom: 10px;
}
li{
position: relative;
width: 100px;
height: 30px;
background: #abcdef;
margin: 10px auto 0px auto; /* 上右下左 */
list-style: none;
}
</style>
</head>
<body>
<div class="box1">
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div class="box2"></div>
<script>
var dragDom;
var liList = document.getElementsByTagName('li');
for(var i = 0; i< liList.length;i++) {
liList[i].setAttribute('draggable',true);
liList[i].ondragstart = function(e) {
console.log(e.target);
dragDom = e.target;
}
}
var box2 = document.getElementsByClassName("box2")[0];
box2.ondragover = function(e) {
e.preventDefault();
}
box2.ondrop = function(e){
box2.appendChild(dragDom);
dragDom = null;
// ondrop:在一个拖动过程中,释放鼠标键时触发此事件
// ondragleave:当被鼠标拖动的对象离开其容器范围内时触发此事件
}
</script>
</body>