-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.scrollpoint.js
67 lines (53 loc) · 2.07 KB
/
jquery.scrollpoint.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
(function ($) {
'use strict';
$.fn.scrollPoint = function (params) {
var $window = $(window);
params = $.extend({
up : false,
down : false,
offsetUp : 0,
offsetDown : 0
}, params);
return this.each(function () {
var up = params.up,
down = params.down,
relativePos = {
isUp : true,
isDown : false,
isIn : false
},
element = $(this);
if (!up && up !== 0) {
up = element.offset().top;
}
if (!down && down !== 0) {
down = up + element.outerHeight();
}
up -= params.offsetUp;
down -= params.offsetDown;
function triggerEvent(eventType, eventParams) {
var n, Event = $.Event(eventType);
for(n in eventParams) {
Event[n] = eventParams[n];
}
element.trigger(Event);
}
function checkScroll() {
var pos = $window.scrollTop(),
oldRelativePos = $.extend({}, relativePos);
relativePos.isUp = pos <= up;
relativePos.isDown = pos >= down;
relativePos.isIn = !relativePos.isUp && !relativePos.isDown;
if (oldRelativePos.isIn !== relativePos.isIn || oldRelativePos.isUp !== relativePos.isUp) {
// If the scroll jumped directly between isUp and isDown
if (oldRelativePos.isIn === relativePos.isIn) {
triggerEvent("scrollPointEnter", relativePos);
}
triggerEvent("scrollPoint" + (relativePos.isIn ? "Enter" : "Leave"), relativePos);
}
triggerEvent("scrollPointMove", relativePos);
}
$window.scroll(checkScroll);
});
};
})(jQuery);