-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflick-scroll.js
More file actions
61 lines (50 loc) · 1.91 KB
/
flick-scroll.js
File metadata and controls
61 lines (50 loc) · 1.91 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
(function($){
var fs = (function(){
function FS(){
var self = this;
// Get viewport size
var viewportHeight = window.innerHeight;
// Setup target
var target = window.scrollY;
// Setup touch lock
var isInteracting = false;
var shouldScroll = false;
// Bind to scroll
$('body').on('touchstart', function(){
shouldScroll = false;
}).on('touchend', function(){
var newY = Math.round(window.scrollY / viewportHeight) * viewportHeight;
target = newY;
shouldScroll = true;
});
// Add additional padding at the end of the page
$('body').height(Math.ceil($('body').height() / viewportHeight) * viewportHeight);
var getFrame = (function()
{
return window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var lastDelta = null;
(function render(){
getFrame(render);
if (window.scrollY != target && shouldScroll) {
var diff = target - window.scrollY;
if (Math.abs(diff) <= 1 || diff == lastDelta) {
window.scrollTo(window.scrollX, target);
} else {
window.scrollTo(window.scrollX, Math.round(window.scrollY + diff / 5));
}
lastDelta = diff;
} else if (shouldScroll) {
shouldScroll = false;
}
})();
};
return FS;
})();
$.flickScroll = fs;
})(jQuery);