Take a look at the Fiddle provided below:
https://jsfiddle.net/y0mj6v2d/5/
I'm currently trying to figure out the most effective approach for determining when to apply or remove a class based on the vertical scroll position.
My goal is to incorporate side panels (potentially containing banners) into my website that will be displayed:
- between the Header and Footer
- to the left and right of the main container
The height of both my header and footer remains consistent across all pages, allowing me to assign a class depending on the ScrollTop position. However, I need the 'Side Panels' to remain within the boundaries of the footer's starting point. In the current example, the fixed class is removed once the Scroll Position combined with the Window height surpasses the Document height. What I aim for is for these panels to reach the top of the Footer and then start scrolling up the page as the user continues scrolling down the footer. This means switching from fixed positioning to absolute and setting bottom: 0.
The challenge I face now lies in calculating this due to:
- The varying height of the main panel throughout the site
- Potential fluctuations in the banner's height
$(function() {
var panels = $(".side-panels");
var pos = panels.offset().top;
$(window).scroll(function() {
var windowpos = $(window).scrollTop() ;
if(windowpos + $(window).height() >= $(document).height()){
panels.removeClass('fixed').addClass('absolute');
}else if(windowpos >= pos){
panels.addClass('fixed');
}else{
panels.removeClass('fixed');
}
});
});
Would you say this method is the most efficient way to achieve this effect, or can you suggest a better and simpler solution?
Any assistance you can provide would be highly appreciated!
Thanks!