-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollyclasser.js
111 lines (71 loc) · 3.03 KB
/
scrollyclasser.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let options = {
// root: document.querySelector('body'),
rootMargin: '0px',
threshold: 0.5
}
let callback = (entries, observer) => {
entries.forEach((entry) => {
//make sure we're intersecting
if(entry.isIntersecting === true && entry.target.dataset.enterAdd ){
// console.log('did entry')
//get all the classes in the data attribute and split them to an array
let classes = entry.target.dataset.enterAdd.split(" ")
//add each one to the class list
classes.forEach((className) => {
entry.target.classList.add(className)
})
}
//make sure we're NOT intersecting
if(entry.isIntersecting === false && entry.target.dataset.exitRemove ){
// console.log('did exit')
//get all the classes in the data attribute and split them to an array
let classes = entry.target.dataset.exitRemove.split(" ")
//add each one to the class list
classes.forEach((className) => {
entry.target.classList.remove(className)
})
}
// console.log(entry.target.classList)
// ScrollyCLASSES PLUS!!
// enable targeting of other elements to add
if(entry.isIntersecting === true && entry.target.dataset.enterTarget ){
//should make this work with multiples in the future
let targetElements = document.getElementsByClassName(entry.target.dataset.enterTarget)
// console.log(targetElements)
//get all the classes in the data attribute and split them to an array
let classes
if(entry.target.dataset.targetAdd){
classes = entry.target.dataset.targetAdd.split(" ")
for( const t of targetElements) {
//add each one to the class list
classes.forEach((className) => {
t.classList.add(className)
})
}
}
//get all the classes in the data attribute and split them to an array
let removeClasses
if(entry.target.dataset.targetRemove){
removeClasses = entry.target.dataset.targetRemove.split(" ")
for( const t of targetElements) {
//add each one to the class list
removeClasses.forEach((className) => {
t.classList.remove(className)
})
}
}
}
});
};
//invoke the observer
let observer = new IntersectionObserver(callback, options);
// let target = document.querySelector('.observe');
// console.log(target)
// observer.observe(target);
let observedElements = document.getElementsByClassName('observe')
console.log(observedElements)
if(observedElements){
for( let element of observedElements){
observer.observe(element);
}
}