Skip to content

Instantly share code, notes, and snippets.

@fahdi
Last active November 3, 2023 20:47
Show Gist options
  • Save fahdi/0397fe2d5575adda91dc063d8c6f7c5b to your computer and use it in GitHub Desktop.
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.
.sticking {
box-shadow: none !important;
}
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