1、draggable:
(1)可以拖拽一个虚幻的方块:
<div class="a" draggable="true"></div><!-- chrome,safari可以使用,火狐不好使;默认false -->
<!-- 默认可以被拖拽的 -->
<a href=""></a>
<img src="" alt="">
(2)拖拽的生命周期,拖拽的组成
<!-- 生命周期:拖拽开始,拖拽进行中,拖拽结束 -->
<!-- 组成:被拖拽的物体,目标区域 -->
(3)拖拽事件:
<!-- 按下物体的瞬间是不会触发事件的 -->
<script>
var oDragDiv = document.getElementsByClassName('a')[0];
// 拖拽的三个事件:
oDragDiv.ondragstart = function (e) {
console.log(e);
}
oDragDiv.ondrag = function (e) {
console.log(e);
}
oDragDiv.ondragend = function (e) {
console.log(e);
(4)拖拽实例:
<style>
*{
margin: 0;
padding: 0;
}
.a{
position: absolute;
width: 100px;
height: 100px;
background-color: #abcdef;
}
</style>
</head>
<body>
<div class="a" draggable="true"></div>
<!-- 拖拽实例: -->
<script>
var oDragDiv = document.getElementsByClassName('a')[0];
var beginX = 0;
var beginY = 0;
oDragDiv.ondragstart = function (e) {
beginX = e.clientX;
beginY = e.clientY;
}
oDragDiv.ondragend = function (e) {
var x = e.clientX - beginX;
var y = e.clientY - beginY;
oDragDiv.style.left = oDragDiv.offsetLeft +x + "px";
oDragDiv.style.top = oDragDiv.offsetTop +y+ "px";
console.log(x);
console.log(oDragDiv.offsetLeft);
console.log(oDragDiv.style.left);
}
</script>
</body>