-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
34 lines (27 loc) · 976 Bytes
/
script.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
class ParallaxCard {
constructor(cardEl) {
this.cardEl = cardEl
this.imageWrapper = cardEl.querySelector('.card__image-wrapper')
this.heightDiff = this.imageWrapper.clientHeight - this.cardEl.clientHeight
}
update() {
const boundingRect = this.cardEl.getBoundingClientRect()
const topOffset = boundingRect.y
const progress = topOffset / window.innerHeight
const offset = this.heightDiff * progress * -1
this.imageWrapper.style.transform = `translate(0, ${offset}px)`
}
}
function initCardParallax() {
const cardEls = document.querySelectorAll('.card')
const cards = Array.from(cardEls).map((cardEl) => new ParallaxCard(cardEl))
function update() {
cards.forEach((card) => card.update())
}
function onScroll() {
requestAnimationFrame(update)
}
window.addEventListener('scroll', onScroll)
requestAnimationFrame(update)
}
initCardParallax()