forked from lijinke666/javascript-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path拖选框.html
311 lines (285 loc) · 8.59 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>拖选框</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body,
html {
height: 100%;
}
body {
font-family: "Microsoft YaHei";
background: #ddd;
}
#move {
border: 2px solid #396;
cursor: -webkit-grab;
position: absolute;
user-select: none;
display: flex;
justify-content: center;
align-items: center;
transition: box-shadow, background-color, transform cubic-bezier(0.075, 0.82, 0.165, 1) .3s;
}
#move:hover {
box-shadow: 0 0 20px rgba(255, 255, 255, .3);
background-color: rgba(0, 0, 0, .15)
}
ul li {
list-style-type: none;
}
a.reset{
flex:1;
text-decoration: none;
position: absolute;
color:#fff;
background-color: #396;
line-height: 50px;
text-align: center;
width:50px;
height:50px;
border-radius: 50%;
z-index: 2;
}
.cursor-item {
position: relative;
z-index: 1;
}
.cursor-item .cursor {
position: absolute;
border-radius: 50%;
background: -webkit-linear-gradient(top, #F63, #696);
cursor: ew-resize;
transition: transform .2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.cursor-item .cursor:hover {
animation: rotate 1s forwards infinite;
}
@keyframes rotate {
from {
transform: scale(1.2) rotate(0deg)
}
to {
transform: scale(1.2) rotate(360deg)
}
}
</style>
</head>
<body>
<div id="move">
<a class="reset" onclick="loadInit()">还原</a>
<div class="cursor-item">
<span class="cursor" data-value="lt"></span>
<span class="cursor" data-value="rt"></span>
<span class="cursor" data-value="ld"></span>
<span class="cursor" data-value="rd"></span>
</div>
</div>
<script>
//方向配置
const [LEFT_TOP,LEFT_DOWN,RIGHT_TOP,RIGHT_DOWN] = ["lt","ld","rt","rd"]
//框框大小配置
const [width,height] = [200,200];
//远点大小配置
const [cursorWidth,cursorHeight] = [20,20];
const cursorConfig = (width) => ({
[LEFT_TOP]: {
left: `-${width / 2}px`,
top: `-${width / 2}px`
},
[LEFT_DOWN]: {
left: `-${width / 2}px`,
bottom: `-${width / 2}px`
},
[RIGHT_TOP]: {
top: `-${width / 2}px`,
right: `-${width / 2}px`,
},
[RIGHT_DOWN]: {
right: `-${width / 2}px`,
bottom: `-${width / 2}px`
}
})
const $ = (element, all = false) => { return all ? document.querySelectorAll(element) : document.querySelector(element) }
function loadInit() {
init({ ele: $("#move"), width, height, cursorWidth, cursorHeight });
}
window.onload = loadInit
window.addEventListener('resize', loadInit)
function init(config = {
ele: $('#move')
}) {
const {ele, width, height, cursorWidth, cursorHeight} = config;
move_init({
ele,
width,
height,
cursorWidth,
cursorHeight
})(cursorConfig);
drop_init({
ele,
})
}
function drop_init(options = {}) {
let isDown = false;
let items = Array.from($('.cursor', true));
let moveBox = options.ele;
let [offsetX, offsetY, currenX, currenY] = [0, 0, 0, 0];
for (let item of items) {
item.addEventListener('mousedown', function (e) {
e.stopPropagation();
isDown = true;
offsetX = getThisMoueseXY(moveBox, e, offsetX, offsetY).offsetX;
offsetY = getThisMoueseXY(moveBox, e, offsetX, offsetY).offsetY;
currenX = ~~(e.pageX);
currenY = ~~(e.pageY);
})
item.addEventListener('mousemove', function (e) {
e.stopPropagation();
let [moveX, moveY, moveWidth, moveHeight] = [0, 0, 0, 0];
const type = this.getAttribute('data-value');
if (isDown === true) {
moveWidth = ~~(e.pageX - currenX)
moveHeight = ~~(e.pageY - currenY)
moveX = ~~(e.pageX - offsetX)
moveY = ~~(e.pageY - offsetY)
setMoveElementOffset(moveBox, moveX, moveY)
setMoveElementWH(moveBox, moveWidth,moveHeight,type)
setMoveElementWH($('.cursor-item'), moveWidth,moveHeight, type)
}
})
item.addEventListener('mouseup', (e) => {
e.stopPropagation();
isDown = false;
})
item.addEventListener('mouseout', (e) => {
e.preventDefault()
isDown = false;
})
}
}
function move_init(config = {}) {
var deafultConfig = {
width: 100,
height: 100,
cursorWidth: 20,
cursorHeight: 20,
align: "center"
}
var options = Object.assign({}, deafultConfig, config)
const {ele, align, width, height, cursorWidth, cursorHeight} = options;
ele.style.width = $('.cursor-item').style.width = `${width}px`;
ele.style.height = $('.cursor-item').style.height = `${height}px`;
align == "center" && autoCenter(ele)
move(ele)
//设置四个圈圈的大小和位置
return function initCursorPosition(cursorConfig) {
Array.from($(".cursor", true)).map(item => {
item.style.width = `${cursorWidth}px`
item.style.height = `${cursorHeight}px`
const type = item.getAttribute('data-value');
const {left, top, right, bottom} = cursorConfig(cursorWidth)[type];
item.style.left = left
item.style.top = top
item.style.right = right
item.style.bottom = bottom
})
}
}
function move(ele) {
let isMove = false;
var offsetX = 0;
var offsetY = 0;
ele.addEventListener('mousedown', (e) => {
e.preventDefault()
isMove = true;
offsetX = getThisMoueseXY(ele, e, offsetX, offsetY).offsetX;
offsetY = getThisMoueseXY(ele, e, offsetX, offsetY).offsetY;
})
ele.addEventListener('mousemove', (e) => {
e.preventDefault()
var moveX = 0;
var moveY = 0;
if (isMove === true) {
moveX = e.pageX - offsetX;
moveY = e.pageY - offsetY;
setMoveElementOffset(ele, moveX, moveY)
}
})
ele.addEventListener('mouseup', (e) => {
e.preventDefault()
isMove = false;
})
ele.addEventListener('mouseout', (e) => {
e.preventDefault()
isMove = false;
})
}
//获取移动后的鼠标位置
function getThisMoueseXY(target, e, offsetX, offsetY) {
let {left, top} = getBounding(target)
offsetX = ~~(e.pageX - left);
offsetY = ~~(e.pageY - top);
return {
offsetX,
offsetY
}
}
//获取拖拽框的位置信息
function getBounding(ele) {
let { left, top, width, height } = ele.getBoundingClientRect();
return {
left,
top,
width,
height
}
}
//设置拖拽框的位置
function setMoveElementOffset(ele, left, top) {
ele.style.transform = `translate3d(${~~left}px,${~~top}px,0)`
}
//设置拖拽框的大小
function setMoveElementWH(ele, width, height,type) {
width && (ele.style.width = `${Math.max((ele.scrollWidth - (type === RIGHT_TOP ? Math.abs(width) : width) ), cursorWidth)}px`)
height && (ele.style.height = `${Math.max((ele.scrollHeight - (type === RIGHT_TOP ? Math.abs(height) : height) ), cursorHeight)}px`)
}
//垂直居中
function autoCenter(ele) {
let {clientWidth, clientHeight} = document.body;
let {width, height} = getBounding(ele)
let x = ~~((clientWidth - width) / 2)
let y = ~~((clientHeight - width) / 2)
setMoveElementOffset(ele, x, y)
}
//鼠标离开或弹起触发
function mouseOutOrUp(ele, flag) {
ele.addEventListener('mouseup', (e) => {
e.preventDefault()
e.stopPropagation();
flag = false;
})
ele.addEventListener('mouseleave', (e) => {
e.preventDefault()
e.stopPropagation();
flag = false;
})
}
function initConfig() {
$('.cursor-item').style.width = `${width}px`
$('.cursor-item').style.height = `${height}px`
$(".cursor-item li.cursor").style.width = `${cursorWidth}px`;
$(".cursor-item li.cursor").style.height = `${cursorHeight}px`;
}
</script>
</body>
</html>