forked from yeemachine/touchtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
64 lines (49 loc) · 1.69 KB
/
drag.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//-----Click and Drag/Swipe behavior for UI Cards------//
document.querySelectorAll('.card').forEach(e=>{
var object = e,
initX, initY, firstX, firstY;
object.addEventListener('mousedown', function(e) {
if(e.target.classList.contains('play')){toggleDemo()}
e.preventDefault();
initX = this.offsetLeft;
initY = this.offsetTop;
firstX = e.pageX;
firstY = e.pageY;
this.addEventListener('mousemove', dragIt, false);
window.addEventListener('mouseup', function() {
object.removeEventListener('mousemove', dragIt, false);
dragEnd()
}, false);
}, false);
object.addEventListener('touchstart', function(e) {
if(e.target.classList.contains('play')){toggleDemo()}
e.preventDefault();
initX = this.offsetLeft;
initY = this.offsetTop;
var touch = e.touches;
firstX = touch[0].pageX;
firstY = touch[0].pageY;
this.addEventListener('touchmove', swipeIt, false);
window.addEventListener('touchend', function(e) {
e.preventDefault();
object.removeEventListener('touchmove', swipeIt, false);
dragEnd()
}, false);
}, false);
function dragIt(e) {
object.style.transition = "0s"
document.querySelectorAll('.dragSelected').forEach(e=>{e.classList.remove('dragSelected')})
object.classList.add('dragSelected')
this.style.left = initX+e.pageX-firstX + 'px';
this.style.top = initY+e.pageY-firstY + 'px';
}
function dragEnd() {
object.style.transition = ""
}
function swipeIt(e) {
object.style.transition = "0s"
var contact = e.touches;
this.style.left = initX+contact[0].pageX-firstX + 'px';
this.style.top = initY+contact[0].pageY-firstY + 'px';
}
})