-
Notifications
You must be signed in to change notification settings - Fork 15
/
拖拽.html
183 lines (169 loc) · 4.97 KB
/
拖拽.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拖拽</title>
<style>
*{margin:0;padding:0;}
#drag{
width:300px;
height:300px;
background-color:#f99;
cursor:move;
}
.drag1{
width:300px;
height:300px;
background-color:green;
cursor:move;
}
</style>
</head>
<body>
<div id="drag" style="margin-left:0;margin-top:0;"></div>
<div class="drag1"></div>
<script src="jquery.min.js"></script>
<script>
// 1.可以拖拽的元素
// 2.可以拖拽的距离(是否可以超越边界)
// 3.可拖拽的范围
// 4.拖拽时的样式
// var obj = document.getElementById("drag")
// var body = document.getElementsByTagName("body")[0];
// // 使用jQuery获取浏览器可视尺寸
// var bodyWidth = $(document).width();
// var bodyHeight = $(document).height();
// // 使用源生JS获取浏览器可视尺寸
// body.offsetWidth
// obj.onmousedown = function(event){
// X = event.clientX;
// Y = event.clientY;
// var newobj = this.cloneNode(true);
// //初始化影子
// newobj.style.opacity = 0.3;
// newobj.style.position = "absolute";
// newobj.style.top = parseInt(obj.style.marginTop) + "px";
// newobj.style.left = parseInt(obj.style.marginLeft) + "px";
// newobj.style.marginLeft = 0;
// newobj.style.marginTop = 0;
// body.appendChild(newobj)
// var ml = parseInt(newobj.style.left);
// var mt = parseInt(newobj.style.top);
// body.onselectstart = function(){
// return false;
// }
// window.onmousemove = function(event){
// var x = event.clientX;
// var y = event.clientY;
// console.log(y)
// newobj.style.left = x + "px";
// newobj.style.top = y + "px";
// var xn = X - ml;
// var yn = Y - mt;
// newobj.style.left = x - xn + "px";
// newobj.style.top = y - yn + "px";
// if(parseInt(newobj.style.left) < 0){
// newobj.style.left = 0;
// }
// if(parseInt(newobj.style.left) > bodyWidth - newobj.offsetWidth){
// newobj.style.left = bodyWidth - newobj.offsetWidth + "px";
// }
// if(parseInt(newobj.style.top) < 0){
// newobj.style.top = 0;
// }
// if(parseInt(newobj.style.top) > bodyHeight - newobj.offsetHeight){
// newobj.style.top = bodyHeight - newobj.offsetHeight + "px";
// }
// }
// window.onmouseup = function(event){
// obj.style.marginLeft = newobj.style.left;
// obj.style.marginTop = newobj.style.top;
// window.onmousemove = null;
// window.onmouseup = null;
// body.removeChild(newobj)
// }
// }
function drag(option){
var obj = option.obj;
var overflow = option.overflow || false;
var style = option.style || "shadow";
var scope = option.scope || "body";
/*初始化对象样式*/
obj.style.position = "absolute"
obj.style.top = obj.offsetTop + "px";
obj.style.left = obj.offsetLeft + "px";
var body = document.getElementsByTagName("body")[0];
// 使用jQuery获取浏览器可视尺寸
var bodyWidth = $(document).width();
var bodyHeight = $(document).height();
// 使用源生JS获取浏览器可视尺寸
body.offsetWidth
obj.onmousedown = function(event){
X = event.clientX;
Y = event.clientY;
var newobj = this.cloneNode(true);
//初始化影子
if(style == "shadow"){
newobj.style.opacity = 0.3;
}
if(style == "dashed"){
newobj.style.backgroundColor = "transparent";
newobj.style.border = "1px dashed #e3e3e3";
}
newobj.style.position = "absolute";
newobj.style.top = parseInt(obj.style.top) + "px";
newobj.style.left = parseInt(obj.style.left) + "px";
body.appendChild(newobj)
var ml = parseInt(newobj.style.left);
var mt = parseInt(newobj.style.top);
body.onselectstart = function(){
return false;
}
window.onmousemove = function(event){
var x = event.clientX;
var y = event.clientY;
newobj.style.left = x + "px";
newobj.style.top = y + "px";
var xn = X - ml;
var yn = Y - mt;
newobj.style.left = x - xn + "px";
newobj.style.top = y - yn + "px";
if(!overflow){
//边界检测
if(parseInt(newobj.style.left) < 0){
newobj.style.left = 0;
}
if(parseInt(newobj.style.left) > bodyWidth - newobj.offsetWidth){
newobj.style.left = bodyWidth - newobj.offsetWidth + "px";
}
if(parseInt(newobj.style.top) < 0){
newobj.style.top = 0;
}
if(parseInt(newobj.style.top) > bodyHeight - newobj.offsetHeight){
newobj.style.top = bodyHeight - newobj.offsetHeight + "px";
}
}
}
window.onmouseup = function(event){
obj.style.left = newobj.style.left;
obj.style.top = newobj.style.top;
window.onmousemove = null;
window.onmouseup = null;
body.removeChild(newobj)
}
}
}
var a = document.getElementById("drag");
var b = document.getElementsByClassName("drag1")[0];
drag({
obj:a
});
drag({
overflow:true,
obj:b,
style:"dashed",
scope:"abc"
});
</script>
</body>
</html>