-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path原生js可拖拽demo.html
45 lines (40 loc) · 1.1 KB
/
原生js可拖拽demo.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>原生js实现拖拽demo(兼容)</title>
<style type="text/css">
html,body{
padding:0;
margin:0;
overflow:hidden;
}
#obj{
width:100px;
height:100px;
position:absolute;
background-color: #ef4c6c;
cursor: move;
}
</style>
</head>
<body>
<div id="obj"></div>
<script type="text/javascript">
obj.onmousedown = function(e){
var e = e || window.event
, sX = (e.pageX || e.clientX) - parseInt(obj.offsetLeft, 10)
, sY = (e.pageY || e.clientY) - parseInt(obj.offsetTop, 10);
document.body.onmousemove = function(e){
document.body.onmouseup = document.body.onmouseleave = function(e){
document.body.onmousemove = document.body.onmouseup = document.body.onmouseleave = null;
};
with(obj.style){
left = (e.pageX || e.clientX) - sX + "px";
top = (e.pageY || e.clientY) - sY + "px";
}
};
};
</script>
</body>
</html>