0
0
CFCodiga Fan #25142
The code creates a scrollImations function that adds a CSS class to all the boxes in the document. It then creates an IntersectionObserver function that supplies it with the scrollImations function and the options object.
The scrollImations function adds a CSS class to the boxes if the box is entirely inside the observer's view. If the box overlaps some of the observer's view, the function will only add the class if the overlap ratio is 1.0.
// ΡΡΠ½ΠΊΡΠΈΡ Π²ΡΠ΅Π³ΠΎ Π»ΠΈΡΡ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅Ρ CSS-ΠΊΠ»Π°ΡΡ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΈ ΠΎΡΡΡΠ΅ΡΡΠ²Π»ΡΠ΅Ρ Π°Π½ΠΈΠΌΠ°ΡΠΈΡ
const scrollImations = (entries, observer) => {
entries.forEach((entry) => {
// Π°Π½ΠΈΠΌΠΈΡΡΠ΅ΠΌ, Π΅ΡΠ»ΠΈ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠ΅Π»ΠΈΠΊΠΎΠΌ ΠΏΠΎΠΏΠ°Π΄Π°Π΅Ρ Π² ΠΎΡΡΠ»Π΅ΠΆΠΈΠ²Π°Π΅ΠΌΡΡ ΠΎΠ±Π»Π°ΡΡΡ
if(entry.isIntersecting && entry.intersectionRatio == 1) {
entry.target.classList.add('box--visible');
} else {
entry.target.classList.remove('box--visible');
}
});
}
// ΡΠΎΠ·Π΄Π°ΡΠΌ ΠΎΠ±ΡΠ΅ΡΠ²Π΅Ρ Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ
const options = {
threshold: 1.0,
};
const observer = new IntersectionObserver(scrollImations, options);
const boxes = document.querySelectorAll('.box');
boxes.forEach((box) => {
observer.observe(box);
});