-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.js
69 lines (60 loc) · 2.42 KB
/
scroll.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
;(function (window) {
// This function will contain the code
function scrolljsFunc () {
// Enabling strict mode, see developer.mozilla.org
// Only enables strict mode for scroll.js
'use strict'
let _scrolljsObj = {}
_scrolljsObj.init = () => {
_scrolljsObj.parts = document.querySelectorAll('.scrolljs')
_scrolljsObj.num = window.innerHeight / 100
window.addEventListener('scroll', scrolljs.scroll_function)
// Run scroll_function twice to ensure current animation prgress is calulated
_scrolljsObj.scroll_function()
_scrolljsObj.scroll_function()
}
let timer = null;
window.addEventListener('scroll', function () {
if (timer !== null) {
clearTimeout(timer)
}
timer = setTimeout(function () {
_scrolljsObj.scroll_function();
}, 150)
}, false)
// Deal with resizes
window.addEventListener('resize', () => {
_scrolljsObj.num = window.innerHeight / 100})
_scrolljsObj.scroll_function = () => {
for (var x of _scrolljsObj.parts) {
let range = x.getAttribute('data-range').split('to')
// if... then determines if animation should currently run
if (window.pageYOffset > range[0] * _scrolljsObj.num &&
window.pageYOffset < range[1] * _scrolljsObj.num) {
// Calculate current progress through animation
let fraction = (window.pageYOffset - range[0] * _scrolljsObj.num) / (range[1] * _scrolljsObj.num - range[0] * _scrolljsObj.num)
x.style.animationDelay = '-' + fraction.toString() + 's'
} else {
// Even if the item is not in view, ensure it is calculated
if (window.pageYOffset > range[0] * _scrolljsObj.num) {
// Code here is executed if the page has scrolled after the element
x.style.animationDelay = '-0.999s'
} else {
// Code here is executed if the page has not yet scrolled to the element
x.style.animationDelay = '0s'
}
}
}
}
return _scrolljsObj
}
// We need that our library is globally accesible, then save in the window
if (typeof (window.scrolljs) === 'undefined') {
window.scrolljs = scrolljsFunc()
}
})(window) // Send the window variable withing our function
// The scrolljs object is now created. View it with:
// console.log(scrolljs)
if (!(scrolljs.disableAutoInit)) {
window.addEventListener('load', scrolljs.init)
}