-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (29 loc) · 971 Bytes
/
index.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
import $ from 'jquery';
function init({
start,
end,
respire = 0,
}, fn, exceptFn) {
const windowHeight = $(window).height();
const $start = $(start);
const $end = $(end);
const topOfDivStart = $start.offset().top;
const heightStart = $start.outerHeight();
let topOfDivEnd;
if (end !== undefined && $end.length > 0) {
topOfDivEnd = $end.offset().top;
}
$(window).scroll(() => {
const isStart = $(window).scrollTop() > ((topOfDivStart - windowHeight) + (heightStart - respire));
if (end !== undefined && $end.length > 0) {
const isNotEnd = $(window).scrollTop() <= ((topOfDivEnd) - windowHeight);
return (isStart && isNotEnd) ? fn() : exceptFn();
}
return (isStart) ? fn() : exceptFn();
});
}
function scrollTrigger(config, isTriggerFn = () => {}, isNotTriggerFn = () => {}) {
const { length } = $(config.start);
if (length) init(config, isTriggerFn, isNotTriggerFn);
}
export default scrollTrigger;