I need to stop a div which gets a fixed position on scroll before it touches another element, basically make it go back to absolute position at a certain position, and this should be working at any size of the screen. I have a jsfiddle with code and there are two elements, please see the example and help if you know the answer thanks!
var navTop = $('#nav').offset().top;
var lastMode = "absolute";
$(window).scroll(function(){
var mode;
if ($(this).scrollTop() >= navTop) {
mode = 'fixed';
} else {
mode = 'absolute';
}
if(lastMode !== mode) {
if (mode == 'fixed') {
$('#nav').css('position', 'fixed');
$('#nav').css('top', '0');
} else {
$('#nav').css('position', 'absolute');
$('#nav').css('top', navTop);
}
lastMode = mode;
}
});
https://jsfiddle.net/Zygimantas10/vro3LLu9/
let me know if this is not clear.