-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
29 lines (24 loc) · 965 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
const $ = (selector, context = document) => Array(...context.querySelectorAll(selector))
const parallax = (selector = `.js-parallax`, rate = .09) => {
// Create cross browser requestAnimationFrame method:
window.requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(f) {
setTimeout(f, 1000 / 60)
}
let nodes = $(selector)
function parallaxAnimation() {
// get number of pixels document has scrolled vertically
let scrolltop = window.pageYOffset
nodes.forEach(node => {
// move at 9% of scroll rate
node.style.transform = `translate3d(0, ${-scrolltop * rate}px, 0)`
})
}
window.addEventListener('scroll', function() { // on page scroll
requestAnimationFrame(parallaxAnimation) // call parallaxAnimation() on next available screen paint
}, false)
}
export default parallax