-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwipeMe.js
137 lines (102 loc) · 4.06 KB
/
SwipeMe.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
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
$.fn.slideMaster = function( obj ) {
var slideTrack;
var range=0;
var directionTrack;
var previousBgColor;
var minRange=(obj.range!=undefined)?obj.range:0.6;
$(this).on('touchstart mousedown', function(e) {
slideTrack= $(this)[0];
slideTrack.style.transition=" transform 0.05s";
previousBgColor = window.getComputedStyle(slideTrack).getPropertyValue("background-color");
});
$(this).slide(function( direction, offset ) {
var eleWidth = window.getComputedStyle(slideTrack).getPropertyValue("width");
range= ((offset.x/ parseInt(eleWidth ))*100)/100;
if(direction.x==="right"){
slideTrack.style.background=hexToRgba(obj.rightSlideColor,range);
directionTrack="right";
}
else if(direction.x==="left"){
directionTrack="left";
slideTrack.style.background=hexToRgba(obj.leftSlideColor,Math.abs(range));
}
slideTrack.style.transform="translateX("+offset.x+"px)";
});
$(this).on('touchend mouseup', function(e) {
slideTrack.style.transform="translateX(-"+"0px)";
slideTrack.style.background=previousBgColor;
if(Math.abs(range)>minRange){
if(directionTrack==="right"){
obj.rightSlideFunction();
}
else if(directionTrack==="left"){
obj.leftSlideFunction();
}
}
});
}
$.fn.slide = function( callback ) {
var touchDown = false,
originalPosition = null,
$el = $( this );
function swipeInfo( event ) {
if ('undefined' !== typeof event.originalEvent.pageX) {
var x = event.originalEvent.pageX,
y = event.originalEvent.pageY,
dx, dy;
}else{
var x = event.originalEvent.touches[0].pageX,
y = event.originalEvent.touches[0].pageY,
dx, dy;
}
dx = ( x > originalPosition.x ) ? "right" : "left";
dy = ( y > originalPosition.y ) ? "down" : "up";
return {
direction: {
x: dx,
y: dy
},
offset: {
x: x - originalPosition.x,
y: originalPosition.y - y
}
};
}
$el.on( "touchstart mousedown", function ( event ) {
touchDown = true;
if ('undefined' !== typeof event.originalEvent.pageX) {
originalPosition = {
x: event.originalEvent.pageX,
y: event.originalEvent.pageY
};
}else{
originalPosition = {
x: event.originalEvent.touches[0].pageX,
y: event.originalEvent.touches[0].pageY
};
}
} );
$el.on( "touchend mouseup", function () {
touchDown = false;
originalPosition = null;
} );
$el.on( "touchmove mousemove", function ( event ) {
if ( !touchDown ) { return;}
var info = swipeInfo( event );
callback( info.direction, info.offset );
} );
return true;
};
//function to convert hex to rgba
function hexToRgba(hex,alpha){
var c;
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
c= hex.substring(1).split('');
if(c.length== 3){
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c= '0x'+c.join('');
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+alpha+')';
}
throw new Error('Bad Hex');
}