I have a box that needs to stick, and I am using position: fixed CSS for that when it reaches the bottom of the page while scrolling, and then unstick while scrolling back up past that threshold. Below is my jQuery code:
$(window).scroll(function() {
var wHeight = $(window).height();
var stickyTop = $('.threshold').offset().top;
var xxx = stickyTop - wHeight;
var windowTop = $(window).scrollTop();
if ( windowTop > xxx ) {
console.log(windowTop)
$('.box').addClass("sticky")
} else {
$('.box').removeClass("sticky")
}
});
I don't understand why my else state doesn't work. And it's weird that the console.log kicks in late.
Here's a complete pen: https://codepen.io/frontend2020/pen/vYjJBma?editors=1010
Thanks for helping!