Last active
December 2, 2023 18:37
-
-
Save softiconic/f20c4cb67f978c06721a328f6b8135a1 to your computer and use it in GitHub Desktop.
Apply a class to a specific section on a website as the user scrolls.
This file contains hidden or 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
| // Function to handle the intersection of the observed element | |
| function handleIntersection(entries, observer) { | |
| entries.forEach(entry => { | |
| const targetSection = document.getElementById('tetosection'); // Replace 'tetosection' with your section's ID | |
| if (entry.target === targetSection) { | |
| if (entry.isIntersecting) { | |
| targetSection.classList.add('highlightsc'); | |
| } else { | |
| targetSection.classList.remove('highlightsc'); | |
| } | |
| } | |
| }); | |
| } | |
| // Set up the Intersection Observer | |
| const options = { | |
| threshold: 0.5 // Adjust this threshold as needed (0.5 means 50% of the section is visible) | |
| }; | |
| const observer = new IntersectionObserver(handleIntersection, options); | |
| // Start observing the target section | |
| const targetSection = document.getElementById('tetosection'); // Replace 'tetosection' with your section's ID | |
| observer.observe(targetSection); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment