Utilizing jQuery, I have successfully implemented a functionality to change the position of a fixed div at the top of the screen (top:0
) when scrolling reaches a specific point. This involves dynamically switching classes and modifying CSS styles.
However, I am exploring alternative methods for this task. The current approach relies on specifying a fixed height, which poses limitations when triggering the transition 30px away from a content block:
$(function(){
$(document).scroll(function() {
var x = $(this).scrollTop();
if(x > 2025) {
if($(window).width() > 950) {
$('.topFullWidthWhite').addClass('nonStick');
}
} else {
$('.topFullWidthWhite').removeClass('nonStick');
}
});
});
SO...
I'm curious if there's a more flexible approach that could be based on a condition like:
if(x <= 20 from /* HTML ELEMENT */){
//DO WHATEVER HERE
}
It would be great if there is a method that allows this relative positioning with respect to other elements rather than relying on document height alone.
Thank you!