Last active
January 7, 2022 08:17
-
-
Save rimzzlabs/1bcee957ba4bf5b0b37fdc8d93095282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const navbar = document.querySelector(".navbar"), | |
sectionKelas = document.querySelectorAll(".section-kelas"); | |
// initialize IntersectionObserver | |
const io = new IntersectionObserver((entries) => { | |
// entries are array of element e.g: [<div></div>, <p></p>] | |
// loop through the element | |
entries.forEach((entry) => { | |
// assign the data set attribute | |
const kelas = entry.target.dataset.kelas; | |
// let's check if entry is on the viewport | |
if (entry.isIntersecting) { | |
// below here run your condition and start to manipulate navbar | |
if (kelas === "kelas-13") { | |
navbar.style.backgroundColor = "#db0025"; | |
} | |
// if you want to unobserve each element when they're done, uncomment below code | |
// io.unobserve(entry.target); | |
} | |
}); | |
}); | |
// first loop the array of element into one picee | |
sectionKelas.forEach((element) => { | |
// each element of section-kelas, observe with InterSectionObserver | |
io.observe(element); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment