-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.skrulle.js
80 lines (68 loc) · 2.24 KB
/
jquery.skrulle.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
/**
* jQuery Skrulle
*
* By Pelle Bjerkestrand - http://pellebjerkestrand.net/
* License: UNLICENSE - http://unlicense.org/
*/
;(function($){
'use strict';
$.fn.skrulle = function(options){
if(this.length === 0){
return this;
}
if(this.length > 1){
this.each(function(){
$(this).skrulle(options);
});
return this;
}
var $this = $(this);
var defaults = {
selector: 'body',
up: 'up',
down: 'down'
};
var skrulle = {
settings: {},
previousScrollTop: 0,
element: null,
debounce: function(fn, delay) {
// http://remysharp.com/2010/07/21/throttling-function-calls/
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
},
handleScroll: function(oldY, newY){
if(oldY < newY){
// scrolling down
skrulle.element
.removeClass(skrulle.settings.up)
.addClass(skrulle.settings.down);
} else if (oldY > newY){
// scrolling up
skrulle.element
.removeClass(skrulle.settings.down)
.addClass(skrulle.settings.up);
}
skrulle.previousScrollTop = newY;
},
init: function(){
$.extend(skrulle.settings, defaults, options);
if(!skrulle.element){
skrulle.element = $(skrulle.settings.selector);
skrulle.previousScrollTop = $this.scrollTop();
}
$(window).on('scroll', skrulle.debounce(function(){
skrulle.handleScroll(skrulle.previousScrollTop, $this.scrollTop());
}, 150));
}
};
skrulle.init();
return $this;
};
})(jQuery);