-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrolltrigger.js
55 lines (44 loc) · 1.67 KB
/
scrolltrigger.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
// Depends on jQuery
// Runs trigger code once per direction
// Handles effects depending what direction
;var ScrollTrigger = (function (window, $) {
'require strict';
return function (options) {
var $window = $(window);
var triggerDownCallback = options ? options.triggerDownCallback : function () {};
var triggerUpCallback = options ? options.triggerUpCallback : function () {};
var triggerPos = options ? options.triggerPosition : 0;
var lastTriggerDirection = 'up';
var lastPos = $window.scrollTop();
function getDir() {
return $window.scrollTop() >= lastPos ? 'down' : 'up';
}
function isTriggered() {
if (getDir() === 'down') {
return $window.scrollTop() > triggerPos;
} else if (getDir() === 'up') {
return $window.scrollTop() < triggerPos;
}
}
$window.scroll(function () {
if (isTriggered()) {
// only run the code once per trigger direction
switch (getDir()) {
case 'down':
if (lastTriggerDirection === 'up') {
triggerDownCallback();
lastTriggerDirection = 'down';
}
break;
case 'up':
if (lastTriggerDirection === 'down') {
triggerUpCallback();
lastTriggerDirection = 'up';
}
break;
}
}
lastPos = $window.scrollTop();
});
};
})(window, jQuery);