我的菜单都设置好了,可以开始了,而且我现在的JS没有让页面跳到所有地方,这是很好的。我遇到的问题是,让我的“固定”导航向上/向下滑动,而不是凭空出现。
目前,我的jQuery正在激活一个屏幕下大约300px的类,这会将菜单位置从相对更改为固定。
请注意:我最初设法使用静态定位,但在我的网站上使用z-index和其他一些元素时遇到了困难,所以我希望定位保持相对(而不是静态)。我也不想用javascript以任何方式复制菜单(我见过一些这样做的例子)。
当菜单位置变为固定时,如何使用jquery或css来实现向下滑动和向上滑动的效果?
下面是我的代码,非常感谢。
HTML:
<div class="nt-main-navigation-sticky">
<!-- my nav, logo, etc, is in here. -->
</div>CSS:
.nt-main-navigation-sticky {
position: relative;
}
.activeScroll .nt-main-navigation-sticky {
position: fixed;
top: 0;
left: 0;
}JQUERY:
// get my header height
var getHeaderHeight = $('.nt-main-navigation-sticky').outerHeight();
// add/remove body class
$(window).scroll(function() {
if($(window).scrollTop() > getHeaderHeight + 200) {
$('body').addClass('activeScroll').css('padding-top', getHeaderHeight);
} else {
$('body').removeClass('activeScroll').css('padding-top', 0);
}
});发布于 2016-08-18 12:49:09
将过渡添加到您的css:
.nt-main-navigation-sticky {
position: relative;
}
.activeScroll .nt-main-navigation-sticky {
position: fixed;
top: -50px; // or whatever the height of the navbar
left: 0;
transition: top 600ms;
}和js:
if($(window).scrollTop() > getHeaderHeight + 200) {
$('body').addClass('activeScroll').css('padding-top', getHeaderHeight);
$('.nt-main-navigation-sticky').css('top', '0px'); // will trigger transition
} else {
$('.nt-main-navigation-sticky').css('top', '-50px'); // will trigger transition
$('body').removeClass('activeScroll').css('padding-top', 0);
}https://stackoverflow.com/questions/39010104
复制相似问题