Last active
December 2, 2023 18:37
-
-
Save softiconic/378ef276c1bb724f6ef632017abc6263 to your computer and use it in GitHub Desktop.
Change background color on scroll
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 'my-section' 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 'my-section' 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