-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.32 KB
/
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
41
42
43
44
var parallaxItems = [];
var fadeItems = [];
function scrollHandler(e) {
var windowHeight = window.innerHeight;
parallaxItems.forEach(function(item) {
var ratio = parseFloat(item.getAttribute('data-parallax'));
var rect = item.getBoundingClientRect();
if (rect.top < window.innerHeight && rect.bottom > 0) {
var normalized = (rect.top - windowHeight) / (rect.top - rect.bottom - windowHeight);
normalized = Math.max(0, Math.min(1, normalized));
var y = normalized * ratio * rect.height;
item.style.transform = 'translate(0, ' + y + 'px)';
item.style.webkitTransform = 'translate(0, ' + y + 'px)';
}
});
fadeItems.forEach(function(item) {
var rect = item.getBoundingClientRect();
var offset = parseFloat(item.getAttribute('data-fade') || 0);
if (rect.top < (window.innerHeight - offset) && rect.bottom > offset) {
item.style.opacity = 1;
}
});
}
function init() {
var itemsParallax = document.querySelectorAll('[data-parallax]');
var itemsFade = document.querySelectorAll('[data-fade]');
for(var i = 0; i < itemsParallax.length; i++) {
parallaxItems.push(itemsParallax[i]);
}
for(var i = 0; i < itemsFade.length; i++) {
fadeItems.push(itemsFade[i]);
}
scrollHandler();
window.addEventListener('scroll', scrollHandler);
}
window.onload = init;