1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| <!-- js 部分 -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
var dragging = false;
var position = null;
dragBox.addEventListener('mousedown',function(e){
dragging = true;
position = [e.clientX,e.clientY];
});
document.addEventListener('mouseover',function(e){
if(dragging === false) return null;
const x = e.clientX;
const y = e.clientY;
const deltaX = x -position[0];
const deltaY = y - position[1];
const left = parseInt(dragBox.style.left || 0);
const top = parseInt(dragBox.style.top || 0);
dragBox.style.left = left + deltaX + 'px';
dragBox.style.top = left + deltaY + 'px';
position = [x,y];
})
document.addEventListener('mouseup',function(e){
dragging = false;
})
</script>
|