-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinertialSliderScript.js
More file actions
354 lines (270 loc) · 14.2 KB
/
inertialSliderScript.js
File metadata and controls
354 lines (270 loc) · 14.2 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*https://github.com/Shadman77, https://github.com/Shadman77/HTML-Card-Slider*/
/*
MIT License
Copyright (c) 2018 Shadman Saif Anonno
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
window.onload = function () {
/*Test via a getter in the options object to see if the passive property is accessed*/
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function () {
supportsPassive = true;
}
});
window.addEventListener("testPassive", null, opts);
window.removeEventListener("testPassive", null, opts);
} catch (e) { }
var pressed = false,
scrollingAmount = 0;
var previousTime, timeDifference, timeIntervalMomentumScroll;
var pixelsDifference, currentScrollObj;
var previousPosition;
var containers = document.getElementsByClassName('tileSliderContainer');
var leftButtons = document.getElementsByClassName('leftArrowButton');
var rightButtons = document.getElementsByClassName('rightArrowButton');
/*Adding event listeners*/
for (var i = 0; i < containers.length; i++) {
containers[i].addEventListener('mousedown', startSliding, supportsPassive ? { passive: true } : false);
containers[i].addEventListener('touchstart', startSliding, supportsPassive ? { passive: true } : false);
containers[i].addEventListener('mousemove', sliding, supportsPassive ? { passive: true } : false);
containers[i].addEventListener('touchmove', sliding, supportsPassive ? { passive: true } : false);
containers[i].addEventListener('mouseup', endSliding, supportsPassive ? { passive: true } : false);
containers[i].addEventListener('touchend', endSliding, supportsPassive ? { passive: true } : false);
}
for (var i = 0; i < rightButtons.length; i++) {
rightButtons[i].addEventListener('click', rightButtonScroll);
}
for (var i = 0; i < leftButtons.length; i++) {
leftButtons[i].addEventListener('click', leftButtonScroll);
}
window.addEventListener("resize", getAmountToScroll);
/*Getting the amount we need to scroll using the buttons*/
function getAmountToScroll() {
/*
There must be at-least two tiles/cards in the first container for this to work.
We calculate the amount by getting the difference of the first two tiles's horizontal start position.
*/
var x = document.getElementsByClassName('tiles')[1].offsetLeft - document.getElementsByClassName('tiles')[0].offsetLeft;
var y = Math.floor((document.body.clientWidth - document.getElementsByClassName('tiles')[0].offsetLeft) / x);
scrollingAmount = x * y;
/**
* If the size of a single card/tile is larger than the width then
* scroll amount = difference between the offset of two adjacent cards/tiles
*/
if(scrollingAmount == 0)
{
scrollingAmount = x;
}
}
getAmountToScroll();
/*When the user pressed down the mouse button or when touch was initiated*/
function startSliding(event) {
/*If the sliding sequence already did not start*/
if (!pressed) {
/*To end the momentum scrolling function if it has already not ended*/
clearInterval(timeIntervalMomentumScroll);
pixelsDifference = 0;
/*Getting the current time in milliseconds*/
var date = new Date();
previousTime = date.getTime();
/*To prevent smooth scrolling*/
event.currentTarget.style.scrollBehavior = "";
/*Prevent Element Dragging*/
window.ondragstart = function () { return false; };
/*If it is a touch event instead of a mouse event*/
if (event.type == 'touchstart')
event = event.touches[0];
/*Getting start of slide position*/
previousPosition = event.clientX;
/*The sliding sequence has started*/
pressed = true;
}
}
/*While the mouse button is pressed down or touch is continuing*/
function sliding(event) {
/*If the sliding sequence already started*/
if (pressed) {
/*Getting the current time in milliseconds*/
var date = new Date();
var timeMs = date.getTime();
var event2 = event;
/*If it is a touch event instead of a mouse event*/
if (event.type == 'touchmove')
event2 = event.touches[0];
/*Scrolling the required amount*/
event.currentTarget.scrollLeft = event.currentTarget.scrollLeft + previousPosition - event2.clientX;
/*Calculating the difference between the current and previous cursor/touch position*/
if (event2.clientX != previousPosition) {
timeDifference = timeMs - previousTime;
pixelsDifference = event2.clientX - previousPosition;
}
/*Storing the current cursor position and current time*/
previousPosition = event2.clientX;
previousTime = timeMs;
}
}
/*When the mouse button is released down or touch is discontinued*/
function endSliding(event) {
/*Setting draggable to true since we are done sliding */
window.ondragstart = function () { return true; };
/*Sliding sequence ended*/
pressed = false;
/*We get 16ms from the equation floor function of 1000ms/desired frame rate(60)*/
/*If there are performance issues related to the momentumScroll function then reducing the frame rate to 30 may help*/
/*If that does not work then deleting all the code from the next line to the end of this function will help, but there won't be any inertial/momentum scrolling effect
*/
/*Storing the event generating object*/
currentScrollObj = event.currentTarget;
/*Calculating the required pixel difference for the given time interval of 16ms(60 frames per sec)*/
pixelsDifference = Math.floor(pixelsDifference / timeDifference * 16); //so that pixelDifference is an integer
/*Setting the momentumScroll function to run at the given time intervals*/
timeIntervalMomentumScroll = setInterval(momentumScroll, 16);
}
/*To allow inertial/momentum scroll effect*/
function momentumScroll() {
/*Determining the scroll distance for the current iteration*/
/*The constant 0.88 determines the rate at which the speed slows down*/
pixelsDifference *= 0.88;
/*Check if the scroll distance has reduced to 0 or below*/
if (pixelsDifference < 1 && pixelsDifference > -1) {
clearInterval(timeIntervalMomentumScroll);
} else {
/*Applying the required scroll to the required container*/
currentScrollObj.scrollLeft -= pixelsDifference;
}
}
/*When the right arrow button is pressed*/
function rightButtonScroll(event) {
/**Clearing previous time intervals to prevent malfunction */
clearInterval(scrollUtilTimer);
/*To end the momentum scrolling function if it has already not ended*/
clearInterval(timeIntervalMomentumScroll);
var currentScrollAmount, previousScrollAmount = 0;
/*Getting the required container*/
var element = event.currentTarget.previousElementSibling;
/**If IE or Edge */
if (navigator.userAgent.indexOf('MSIE') !== -1
|| navigator.appVersion.indexOf('Trident/') > -1 || navigator.userAgent.indexOf('Edge') >= 0) {
scrollUtilTimer = setInterval(function () {
//Amount to scroll substracted by the amount that is already scrolled
/**Horizontal scroll distance, vertical scroll distance DOM object, horizontal scroll direction, vertical scroll direction */
currentScrollAmount = (Math.floor(element.scrollLeft / scrollingAmount) + 1) * scrollingAmount - element.scrollLeft;
//If we reached full scroll
if (currentScrollAmount == previousScrollAmount) {
clearInterval(scrollUtilTimer);
}
scrollUtil(currentScrollAmount, 0, element, 1, 1);
/**Storing the privious scroll amount */
previousScrollAmount = currentScrollAmount;
}, 16);
} else {//if other browsers
/*To allow smooth scrolling when button pressed*/
element.style.scrollBehavior = "smooth";
/*Scrolling the required amount*/
element.scrollLeft = (Math.floor(element.scrollLeft / scrollingAmount) + 1) * scrollingAmount;
}
}
/*When the right arrow button is pressed*/
function leftButtonScroll(event) {
/**Clearing previous time intervals to prevent malfunction */
clearInterval(scrollUtilTimer);
/*To end the momentum scrolling function if it has already not ended*/
clearInterval(timeIntervalMomentumScroll);
/*Getting the required container*/
var element = event.currentTarget.nextElementSibling;
var currentScrollAmount, previousScrollAmount = 0;
/**If scrolled amount is a multiple of the required scrolling amount */
if (element.scrollLeft % scrollingAmount == 0) {
if (navigator.userAgent.indexOf('MSIE') !== -1
|| navigator.appVersion.indexOf('Trident/') > -1 || navigator.userAgent.indexOf('Edge') >= 0) {
scrollUtilTimer = setInterval(function () {
/**Calculating the required scroll amount */
if (element.scrollLeft <= scrollingAmount)
currentScrollAmount = element.scrollLeft;
else
currentScrollAmount = element.scrollLeft - scrollingAmount;
//If we reached full scroll
if (currentScrollAmount == previousScrollAmount) {
clearInterval(scrollUtilTimer);
}
/**Horizontal scroll distance, vertical scroll distance DOM object, horizontal scroll direction, vertical scroll direction */
scrollUtil(currentScrollAmount, 0, element, -1, 1);
/**Storing the previous scrolling amount */
previousScrollAmount = currentScrollAmount;
}, 16);
}
else {
/*To allow smooth scrolling when button pressed*/
element.style.scrollBehavior = "smooth";
/**Scroll required amount */
element.scrollLeft -= scrollingAmount;
}
}
else {
/**If IE or Edge */
if (navigator.userAgent.indexOf('MSIE') !== -1
|| navigator.appVersion.indexOf('Trident/') > -1 || navigator.userAgent.indexOf('Edge') >= 0) {
scrollUtilTimer = setInterval(function () {
//Amount to scroll substracted by the amount that is already scrolled
/**Horizontal scroll distance, vertical scroll distance DOM object, horizontal scroll direction, vertical scroll direction */
currentScrollAmount = element.scrollLeft - (Math.floor(element.scrollLeft / scrollingAmount) * scrollingAmount);
//If we reached full scroll
if (currentScrollAmount == previousScrollAmount) {
clearInterval(scrollUtilTimer);
}
scrollUtil(currentScrollAmount, 0, element, -1, 1);
/**Storing the previous scroll amount */
previousScrollAmount = currentScrollAmount;
}, 16);
}
else {
/*To allow smooth scrolling when button pressed*/
element.style.scrollBehavior = "smooth";
/**Scroll required amount */
element.scrollLeft = Math.floor(element.scrollLeft / scrollingAmount) * scrollingAmount;
}
}
}
};
var scrollUtilTimer;//Variable for time intervals for the scrollUtil function
/**x = horizontal scroll amount, y is for vertical, obj = object that needs scrolling, xdir, ydir = 1 for positive direction and -1 for negative direction*/
function scrollUtil(x, y, obj, xDir, yDir) {
/**So that smooth scrolling works in Internet explorer as well*/
var xmod = x % 20;
var ymod = y % 20;
if (x > xmod && y > ymod) {
obj.scrollTop = obj.scrollTop + 20 * yDir;
obj.scrollLeft = obj.scrollLeft + 20 * xDir;
x = x - 20;
y = y - 20;
}
else if (x > xmod) {
obj.scrollLeft = obj.scrollLeft + 20 * xDir;
x = x - 20;
}
else if (y > ymod) {
obj.scrollTop = obj.scrollTop + 20 * yDir;
y = y - 20;
}
else {
obj.scrollTop = obj.scrollTop + ymod * yDir;
obj.scrollLeft = obj.scrollLeft + xmod * xDir;
clearInterval(scrollUtilTimer);
}
}