-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy path3、模块化开发拖拽效果.html
63 lines (56 loc) · 1.65 KB
/
3、模块化开发拖拽效果.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div{width: 100px;height: 100px;position: absolute;background: orange;top:0;left: 0;cursor: move;}
#div2{ top:100px;left: 160px;background: blueviolet;}
#div3{ top:30px;left: 360px;background: chocolate;}
</style>
</head>
<body>
<div id="div1"><img src="logo.jpg" alt="logo"/></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
<script src="event3.js"></script>
<script>
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var oDiv3=document.getElementById("div3");
on(oDiv1,"mousedown",down);
on(oDiv2,"mousedown",down);
on(oDiv3,"mousedown",down);
function down(e){
this.x=this.offsetLeft;
this.y=this.offsetTop;
this.mouseX= e.pageX;
this.mouseY= e.pageY;
if(this.setCapture){
this.setCapture();
on(this,"mousemove",move);
on(this,"mouseup",up);
}else{
this._move=bindThis(this,move);
this._up=bindThis(this,up);
on(document,"mousemove",this._move);
on(document,"mouseup",this._up);
}
}
function move(e){
this.style.left=this.x+ (e.pageX-this.mouseX)+"px";
this.style.top=this.y+ (e.pageY-this.mouseY)+"px";
}
function up(e){
if(this.releaseCapture){
this.releaseCapture();
off(this,"mousemove",move);
off(this,"mouseup",up);
}else{
off(document,"mousemove",this._move);
off(document,"mouseup",this._up);
}
}
</script>