Currently, I have successfully implemented the functionality to stick the div
to the top once it scrolls down by 320px
. However, I am curious if there is an alternative approach to achieving this effect. Below is the code snippet I am using:
jQuery(function($) {
function fixDiv() {
if ($(window).scrollTop() > 320) {
$('#navwrap').css({ 'position': 'fixed', 'top': '0', 'width': '100%' });
}
else {
$('#navwrap').css({ 'position': 'static', 'top': 'auto', 'width': '100%' });
}
}
$(window).scroll(fixDiv);
fix5iv();
});
The current implementation works fine, but the issue arises when the height of other preceding divs
varies, making relying on the fixed 320px
problematic. Is there a way to achieve this without using
if ($(window).scrollTop() > 320)
, so that it fades in at the top after scrolling past the div #navwrap
?