-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (58 loc) · 1.89 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
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
/**
* Entry for simple-scroll-intersection demo.
*
* Moves four elements around on scroll, updates them on dis/intersection.
*
* Copyright (c) 2017-2025 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
import { createSimpleScrollIntersection } from './simple-scroll-intersection';
function updateElement (element, result) {
const direction = Object.keys(result.from).filter(key => result.from[key]);
element.classList.toggle('intersected');
if (result.intersection) {
this.directionClass = `${direction}-x`;
element.classList.add(this.directionClass);
} else {
element.classList.remove(this.directionClass);
}
}
window.addEventListener('DOMContentLoaded', () => {
let scrollTick = false;
const scrollSelector = 'body';
const targetSelector = '.target';
const ssiUpdates = [];
const {
top, right, bottom, left
} = ['.top', '.right', '.bottom', '.left'].reduce((prev, curr) => {
const acc = prev;
const el = document.querySelector(curr);
ssiUpdates.push(createSimpleScrollIntersection({
scrollSelector,
movingSelector: curr,
target: targetSelector,
notify: updateElement.bind({}, el)
}).getUpdateScroll());
acc[curr.substring(1)] = el;
return acc;
}, {});
function updateScroll () {
const y = window.scrollY;
top.style.transform = `translate3d(-50%, ${y * 1.5}px, 0)`;
right.style.transform = `translate3d(${-(y * 1.5)}px, -50%, 0)`;
bottom.style.transform = `translate3d(-50%, ${-y}px, 0)`;
left.style.transform = `translate3d(${y}px, -50%, 0)`;
ssiUpdates.forEach(update => update());
}
window.addEventListener('scroll', () => {
if (!scrollTick) {
scrollTick = true;
requestAnimationFrame(() => {
updateScroll();
scrollTick = false;
});
}
}, {
passive: true
});
});