In my container there are sections/boxes, but the last one of these boxes should follow scrolling when none of the other boxes are visible.
So, when user sc
I wrote a relatively simple answer for this.
I have a table that's using one of the "sticky table header" plugins to stick right below a particular div on my page, but the menu to the left of the table didn't stick (as it's not part of the table.)
For my purposes, I knew the div that needed "stickiness" was always going to start at 385 pixels below the top of the window, so I created an empty div right above that:
<div id="stopMenu" class="stopMenu"></div>
Then ran this:
$(window).scroll(function(){
if ( $(window).scrollTop() > 385 ) {
extraPadding = $(window).scrollTop() - 385;
$('#stopMenu').css( "padding-top", extraPadding );
} else {
$('#stopMenu').css( "padding-top", "0" );
}
});
As the user scrolls, it adds whatever the value of $(window).scrollTop()
is to the integer 385
, then adds that value to the stopMenu
div that's above the thing I want to stay focused.
In the event the user scrolls all the way back up, I just set the extra padding to 0.
This doesn't require the user to do anything IN CSS particularly, but it's kind of a nice effect to make a small delay, so I put the class="stopMenu"
in as well:
.stopMenu {
.transition: all 0.1s;
}
I needed the div to stop when it reach a certain object, so i did it like this:
var el = $('#followdeal');
var elpos_original = el.offset().top;
$(window).scroll(function(){
var elpos = el.offset().top;
var windowpos = $(window).scrollTop();
var finaldestination = windowpos;
var stophere = ( $('#filtering').offset().top ) - 170;
if(windowpos<elpos_original || windowpos>=stophere) {
finaldestination = elpos_original;
el.stop().animate({'top':10});
} else {
el.stop().animate({'top':finaldestination-elpos_original+10},500);
}
});
This is my final code .... (based on previous fixes, thank you big time for headstart, saved a lot of time experimenting). What bugged me was scrolling up, as well as scrolling down ... :)
it always makes me wonder how jquery can be elegant!!!
$(document).ready(function(){
//run once
var el=$('#scrolldiv');
var originalelpos=el.offset().top; // take it where it originally is on the page
//run on scroll
$(window).scroll(function(){
var el = $('#scrolldiv'); // important! (local)
var elpos = el.offset().top; // take current situation
var windowpos = $(window).scrollTop();
var finaldestination = windowpos+originalelpos;
el.stop().animate({'top':finaldestination},500);
});
});
That code doesn't work very well i fixed it a little bit
var el = $('.caja-pago');
var elpos_original = el.offset().top;
$(window).scroll(function(){
var elpos = el.offset().top;
var windowpos = $(window).scrollTop();
var finaldestination = windowpos;
if(windowpos<elpos_original) {
finaldestination = elpos_original;
el.stop().animate({'top':400},500);
} else {
el.stop().animate({'top':windowpos+10},500);
}
});