-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetCapture()
69 lines (56 loc) · 1.18 KB
/
setCapture()
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!Doctype HTML>
<html>
<head>
<meta charset='utf-8'>
<title>无</title>
<style>
#div1{
width: 50px;
height: 100px;
background: red;
position: absolute;/*必须加 绝对定位*/
}
#div2{
margin: 300px 300px;
width: 100px;
height: 100px;
background: blue;
position: absolute;
}
#img1{
position: absolute;
}
</style>
<script>
window.onload = function (){
var oDiv1 = document.getElementById('div1');
var oImg = document.getElementById('img1');
oImg.onmousedown = function (ev){
var ev = ev || event;
var disX = ev.clientX - this.offsetLeft;
var disY = ev.clientY - this.offsetTop;
if( oImg.setCapture){
oImg.setCapture();/*设置全局捕获*/
}
document.onmousemove = function(ev) {
var ev = ev || event;
oImg.style.left = ev.clientX - disX + 'px';
oImg.style.top = ev.clientY - disY + 'px';
}
document.onmouseup = function (){
document.onmousemove = document.onmouseup = null;
if(oImg.releaseCapture){
oImg.releaseCapture();
}
}
return false;/*阻止默认行为*/
}
}
</script>
</head>
<body>
sdfdfsdf
<div id="div1"></div>
<img id="img1" src="images/1.jpg" alt="">
</body>
</html>