Skip to content

Instantly share code, notes, and snippets.

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