【发布时间】:2015-05-27 15:32:59
【问题描述】:
当用户向下滚动页面时,我的页面顶部出现了一个粘性导航栏,此时它突然弹出/出现在页面上。使用来自How to build floating menu bar when scroll down的以下代码
//jQuery Code
//Sticky Nav
var top = jQuery(‘#mydiv’).offset().top - parseFloat(jQuery(‘#mydiv’).css('marginTop').replace(/auto/, 100));
jQuery(window).scroll(function (event) {
// what the y position of the scroll is
var y = jQuery(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
jQuery(‘#mydiv’).addClass('fixed');
} else {
// otherwise remove it
jQuery(‘#mydiv’).removeClass('fixed');
}
});
//CSS
#mydiv.fixed {
position: fixed;
top: 0;
left: 0;
z-index: 1;
width: 100%;
border-bottom: 1px solid #000;
padding: 10px 0px 0px 3%;
background:#FFFFFF;
font-size: 85% !important;
}
我想给它添加一个过渡效果,让它在出现时淡入,在菜单消失时淡出。
我尝试将主线 jQuery 代码更新为
//whether that's below the form
if (y >= top) {
// if so, ad the fixed class
//jQuery(‘#mydiv’).addClass('fixed');
jQuery(‘#mydiv’).css({'position': 'fixed', 'top': '0px', 'opacity':'0', 'left':'0', 'z-index':'1', 'width':'100%', 'border-bottom':'1px solid #000', 'padding':'10px 0px 0px 3%', 'background':'#FFFFFF'}).animate({opacity:1},300);
当导航出现时它会转换/淡入,但只要有滚动,菜单就会出现故障/闪烁。
如果有人能告诉我如何正确地做到这一点,任何帮助都会很棒
【问题讨论】: