-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScrollerModule.js
More file actions
336 lines (299 loc) · 10.2 KB
/
ScrollerModule.js
File metadata and controls
336 lines (299 loc) · 10.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
/**
* ScrollerModule
* realizza lo scroller verticale con effetto accellerazione e "molla"
*
* @Author: Burgassi Matteo
* @Last Author: Stefano Sergio
*/
Refuel.define('ScrollerModule', {inherits: 'Events'},
function ScrollerModule() {
var self = this;
var delta = 0, oldDelta = 0, index = 0, newY = 0, point = 0, startTime = 0, duration = 0;
var isVertSwipe = false;
var moveArray = [];
var scrollBar, docHeight, pagHeight, now, element, rootDOMElement;
var defaultEvents = ['touchstart','touchmove','touchend','webkitTransitionEnd','msTransitionEnd','oTransitionEnd','transitionend','DOMSubtreeModified','DOMNodeInserted','DOMNodeRemoved','DOMNodeRemovedFromDocument','DOMNodeInsertedIntoDocument','DOMAttrModified','DOMCharacterDataModified'];
var config = {
startScroll: 0,
speed: 1000,
inertia: 300,
maxElong: 150,
scrollBar: false,
topMargin: 0
};
this.init = function(myConfig) {
config = Refuel.mix(config, myConfig);
rootDOMElement = config.rootDOMElement;
if (!rootDOMElement && config.rootId) rootDOMElement = document.querySelector("#"+config.rootId);
resetElement();
docHeight = element.getBoundingClientRect().height;
pagHeight = rootDOMElement.getBoundingClientRect().height;
if (config.scrollBar) createScrollbar(element);
//attach events
defaultEvents.forEach(function(ev) {
element.addEventListener(ev, handleEvent, false);
});
window.addEventListener('resize', handleEvent, false);
};
this.moveTo = function(y, time) {
newY = y;
applyStyle(element, 'transform', 'translate3d(0,' + newY + 'px,0)');
var tvalue = time || config.speed + 'ms cubic-bezier(0, 0, 0, 1)';
applyStyle(element, 'transition', tvalue);
}
this.reset = function(myConfig) {
config = Refuel.mix(config, myConfig);
resetElement();
scrollBarEnd(0);
index, newY, oldDelta, delta = 0;
};
this.destroy = function() {
defaultEvents.forEach(function(ev) {
element.removeEventListener(ev, handleEvent, false);
});
window.removeEventListener('resize', handleEvent, false);
if(scrollBar) scrollBar.parentElement.removeChild(scrollBar);
}
function handleEvent(e) {
switch (e.type) {
case 'touchstart': onTouchStart.call(self, e); break;
case 'touchmove': onTouchMove.call(self, e); break;
case 'touchend': onTouchEnd.call(self, e); break;
case 'webkitTransitionEnd':
case 'msTransitionEnd':
case 'oTransitionEnd':
case 'transitionend': transitionEnd.call(self, e); break;
case 'resize':
case 'DOMSubtreeModified':
case 'DOMNodeInserted':
case 'DOMNodeRemoved':
case 'DOMNodeRemovedFromDocument':
case 'DOMNodeInsertedIntoDocument':
case 'DOMAttrModified':
case 'DOMCharacterDataModified': update.call(self, e);
}
};
function createScrollbar() {
if (scrollBar) {
scrollBar.style.height = Math.floor((pagHeight*pagHeight)/docHeight) + "px";
if (docHeight <= pagHeight) return;
}
if (rootDOMElement) {
if(!scrollBar) {
scrollBar = document.createElement("div");
if (element) {
if (docHeight != 0) {
scrollBar.style.height = Math.floor((pagHeight*pagHeight)/docHeight) + "px";
}
}
//scrollBar.setAttribute("id", config.divID + "-scrollBar");
scrollBar.style.width = "5px";
scrollBar.style.position = "absolute";
scrollBar.style.top = "0px";
scrollBar.style.right = "5px";
scrollBar.style.background = "black";
scrollBar.style.border = "1px solid white";
scrollBar.style.opacity = "0.5";
scrollBar.style.borderRadius = "5px";
scrollBar.style.display = "none";
scrollBar.style.zIndex = 1;
//calcolo altezza della scrollbar
rootDOMElement.appendChild(scrollBar);
}
}
}
function transitionEnd(e) {
var height = element.offsetHeight - rootDOMElement.offsetHeight;
var elementHeight = -height;
if (newY > 0 || (height < 0)) {
fixToUpperBound();
}
else if ((Math.abs(newY) >= Math.abs(height))) {
fixToLowerBound(elementHeight);
}
else if (scrollBar) {
scrollBar.style.display = "none";
}
this.notify('transitionEnd');
};
this.setup = function() {
window.scroll(0,0);
transitionEnd.call(this);
};
function update() {
docHeight = element.getBoundingClientRect().height;
pagHeight = rootDOMElement.getBoundingClientRect().height;
if (scrollBar) createScrollbar();
};
function onTouchStart(e) {
//retrieve animation start point
point = e.touches[0].pageY;
moveArray = [];
//calculate actual delta to stop animation
var pos = Math.ceil(element.getBoundingClientRect().top - rootDOMElement.getBoundingClientRect().top);
applyStyle(element, 'transform', 'translate3d(0,' + pos + 'px,0)');
applyStyle(element, 'transition', '0ms linear');
applyStyle(element, 'transition', 'none');
newY = pos;
index = pos;
// set initial timestamp of touch sequence
startTime = Number( new Date() );
delta = 0;
e.stopPropagation();
// e.preventDefault();
};
function onTouchMove(e) {
if (scrollBar) scrollBar.style.display = "block";
// ensure scroll with one touch and not pinching
if (e.touches.length > 1 || e.scale && e.scale !== 1) return;
//avoid standard behaviour
e.preventDefault();
//calculate vertical scroll
delta = e.touches[0].pageY - point;
newY = index + delta;
//controllo se si cambia direzione
if (Math.abs(oldDelta) >= Math.abs(delta)) {
startTime = Number( new Date() );
point = e.touches[0].pageY;
index = newY;
}
oldDelta = delta;
// translate immediately 1-to-1
applyStyle(element, 'transform', 'translate3d(0,' + newY + 'px,0)');
scrollBarMove(newY);
//block event here
//e.stopPropagation();
moveArray.push(newY);
};
function applyStyle(dom, name, value) {
var atr = Modernizr.prefixed(name);
dom.style[atr] = value;
}
function onTouchEnd(e) {
now = Number(new Date());
duration = now - startTime; //durata dello swipe
var v = delta/duration;
var dist = Math.abs(delta);
isVertSwipe = (Math.abs(moveArray[moveArray.length-1]-moveArray[moveArray.length-2])); //determine if is a vertical swipe
//determine if is too faster to be a swipe;
v >= 8? v=4: "";
v < -8? v=-4: "";
var height = element.offsetHeight - rootDOMElement.offsetHeight;
var elementHeight = -height;
var eventType;
var elong = Math.floor(config.maxElong % (Math.abs(v) * 100)) + 50;
if (isVertSwipe > 3 && dist > 10) {
newY += Math.ceil((v)*config.speed/2);
//check upper bound
if (newY > 0) {
applyStyle(element, 'transform', 'translate3d(0,' + (elong) + 'px,0)');
applyStyle(element, 'transition', elong + 'ms linear');
scrollBarEnd(elong);
eventType = 'upperBoundReached';
}//check lower bound
else if ((Math.abs(newY) >= Math.abs(height))) {
var tvalue = 'translate3d(0,' + (-(height + elong)) + 'px,0)';
applyStyle(element, 'transform', tvalue);
applyStyle(element, 'transition', elong + 'ms linear');
scrollBarEnd(-(height + elong));
eventType = 'lowerBoundReached';
}
else {
moveTo(newY);
scrollBarEnd(newY);
eventType = 'movedTo';
}
}
else {
if ((newY > 0) || (height < 0)) {
//fix position at upper bound
fixToUpperBound();
//block event here
e.stopPropagation();
return;
}
if ((Math.abs(newY) >= Math.abs(height))) {
//fix position at lower bound
fixToLowerBound(elementHeight);
//block event here
e.stopPropagation();
return;
}
if (scrollBar) scrollBar.style.display = "none";
}
oldDelta = 0;
//set the new starting point
index = newY;
e.stopPropagation();
if (eventType) this.notify(eventType, {y: index});
};
function fixToUpperBound(){
applyStyle(element, 'transform', 'translate3d(0,0,0)');
var tvalue = config.inertia + 'ms cubic-bezier(0, 0, 0, 1)';
applyStyle(element, 'transition', tvalue);
if (scrollBar) {
scrollBarEnd(0);
setTimeout(function() {scrollBar.style.display = "none"}, config.inertia);
}
index = 0;
};
function fixToLowerBound(elementHeight){
var tvalue = 'translate3d(0,' + (elementHeight - config.topMargin) + 'px,0)';
applyStyle(element, 'transform', tvalue);
tvalue = config.inertia + 'ms cubic-bezier(0, 0, 0, 1)';
applyStyle(element, 'transition', tvalue);
if (scrollBar) {
scrollBarEnd(elementHeight);
setTimeout(function() {scrollBar.style.display = "none"}, config.inertia);
}
index = elementHeight;
};
function resetElement() {
rootDOMElement = config.rootDOMElement;
if (!rootDOMElement && config.rootId) rootDOMElement = document.querySelector("#"+config.rootId);
if (!rootDOMElement) {
this.error('rootDOMElement not specified'); return;
}
element = rootDOMElement.children[0];
if (!element) {
this.error('scroll pane not specified'); return;
}
rootDOMElement.style.overflow = 'hidden';
element.style.width = '100%';
element.style.position = 'absolute';
element.style.margin = 0;
element.style.listStyle = 'none';
element.style.MozTransform = element.style.webkitTransform = 'translate3d(0,0,0)';
element.style.MozTransitionDuration = element.style.webkitTransitionDuration = 0;
}
function setPosition(y) {
applyStyle(element, 'transition', 'translate3d(0,' + y + 'px,0)');
scrollBarEnd(y);
oldDelta = 0;
delta = 0;
index = y;
newY = y;
};
/*
this.scrollBy = function(y) {
setPosition(index + y);
};
this.scrollToEnd = function() {
setPosition(-(element.offsetHeight - rootDOMElement.offsetHeight));
};
*/
function scrollBarMove(x) {
if (!scrollBar) return;
//transform x document pixel in x page pixel
var factor = pagHeight/docHeight;
var scrollX = Math.floor(x*factor)*(-1);
applyStyle(scrollBar, 'transform', 'translate3d(0,' + (scrollX) + 'px,0)');
}
function scrollBarEnd(x) {
if (!scrollBar) return;
var factor = pagHeight/docHeight;
var scrollX = Math.floor(x*factor)*(-1);
applyStyle(scrollBar, 'transform', 'translate3d(0,' + (scrollX) + 'px,0)');
applyStyle(scrollBar, 'transition', config.speed + 'ms cubic-bezier(0, 0, 0, 1)');
}
});