Last active
November 3, 2023 20:47
-
-
Save fahdi/0397fe2d5575adda91dc063d8c6f7c5b to your computer and use it in GitHub Desktop.
Ah, I understand now. If you want the .sticking to be applied when the header is touching the top (i.e., when its top is at the top of the viewport) and removed otherwise, you can check the getBoundingClientRect of the header.
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
.sticking { | |
box-shadow: none !important; | |
} |
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
let timeout; | |
window.addEventListener('scroll', function() { | |
clearTimeout(timeout); | |
timeout = setTimeout(function() { | |
var header = document.querySelector('header.sticky.on-scroll'); | |
var headerRect = header.getBoundingClientRect(); | |
if (headerRect.top === 0) { | |
header.classList.add('sticking'); | |
} else { | |
header.classList.remove('sticking'); | |
} | |
}, 100); // Adjust the timeout value to find a good balance between responsiveness and performance | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment