1 | import Event from './eventHandler'
|
2 |
|
3 | const drag = (el,downEl)=>{
|
4 | let {innerHeight:height,innerWidth:width} = window;
|
5 | const {clientHeight,clientWidth} = el;
|
6 | const maxLeft = width -clientWidth;
|
7 | const maxTop = height - clientHeight;
|
8 |
|
9 | el.style.position = "fixed";
|
10 | downEl.style.cursor = "move";
|
11 |
|
12 | const _el_cb = (ev)=>{
|
13 | const e = ev||window.event;
|
14 | const posLeft = e.clientX - el.offsetLeft;
|
15 | const posTop =e.clientY - el.offsetTop;
|
16 |
|
17 | const _move_cb = (event)=>{
|
18 | const _e = event||window.event;
|
19 | let curX = _e.clientX - posLeft;
|
20 | let curY = _e.clientY - posTop;
|
21 | curX = curX<=0?0:(curX>=maxLeft?maxLeft:curX);
|
22 | curY = curY<=0?0:(curY>=maxTop?maxTop:curY);
|
23 | el.style.margin = 0;
|
24 | el.style.left = `${curX}px`;
|
25 | el.style.top = `${curY}px`;
|
26 | }
|
27 |
|
28 | const _del_move = ()=>{
|
29 | Event.del(document,"mousemove",_move_cb)
|
30 | Event.del(document,"mouseup",_del_move)
|
31 | downEl.style.cursor = "normal";
|
32 | }
|
33 |
|
34 | Event.add(document,"mousemove",_move_cb)
|
35 |
|
36 | Event.add(document,"mouseup",_del_move)
|
37 |
|
38 | return false;
|
39 | }
|
40 |
|
41 | Event.add(downEl,"mousedown",_el_cb)
|
42 |
|
43 | }
|
44 |
|
45 |
|
46 | export default drag; |
\ | No newline at end of file |