-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo6.js
38 lines (34 loc) · 1.07 KB
/
demo6.js
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
angular
.module('app')
.directive('dragdrop', [function () {
return {
scope: {},
link: (scope, element, attrs) => {
var el = element[0];
el.draggable = true;
el.addEventListener(
'dragstart',
function(e) {
let style = window.getComputedStyle(e.target, null);
e.dataTransfer.setData('text/plain',
(parseInt(style.getPropertyValue('left')) - e.clientX) + ',' + (parseInt(style.getPropertyValue('top')) - e.clientY)
);
// console.log(e.clientX, 'start');
},false
);
document.body.addEventListener('dragover',function(e) {
e.preventDefault();
return false;
},false);
document.body.addEventListener('drop',function(e) {
let offset = e.dataTransfer.getData('text/plain').split(',');
// console.log(e.clientX, 'drop',offset);
el.style.left = (e.clientX + parseInt(offset[0])) + 'px';
el.style.top = (e.clientY + parseInt(offset[1])) + 'px';
e.preventDefault();
return false;
},false);
}
};
}
]);