I am trying to create a fixed div element when the scroll position is between two div elements. I use this code to create a fixed element:
var sidebar = $('.sidebar').offset().top; $(window).scroll(function() { if ($(window).scrollTop() > sidebar) { $('.sidebar').addClass('fixed'); }else{ $('.sidebar').removeClass('fixed'); } });
I do not know how to remove a fixed class when reaching a blue div. I tried to get the current position of the blue div and add it to the if: var blueDiv = $('.bottom').offset().top : statement:
if ($(window).scrollTop() > sidebar && $(window).scrollTop() < blueDiv ){
Fiddle: https://jsfiddle.net/L7p5yeet/
jquery css sticky
Appel
source share