[英]A variation of the fixed top navbar
How do I make a nav bar that on page loading is present at, say, 50px from the top? 如何制作导航栏,该导航栏在页面加载时位于距顶部50像素的位置? As I scroll down, it doesn't move but when I cross it, it behaves similar to a fixed-top nav bar in Bootstrap. 当我向下滚动时,它不会移动,但是当我穿过它时,其行为类似于Bootstrap中的固定顶部导航栏。 It would be really helpful if you used Bootstrap. 如果您使用Bootstrap,那将真的很有帮助。
Thanks in advance for your answers. 预先感谢您的回答。
Use this simple jquery script: 使用以下简单的jquery脚本:
$(window).scroll(function () {
var height = $('body').height();
var scrollTop = $(window).scrollTop();
if(scrollTop>50){
$('#navbar').css({ 'position': 'fixed', 'top' : '0', 'opacity': '0.5'});
} else {
$('#navbar').css({ 'position': 'absolute', 'top': '50px', 'opacity': '1'});
}
});
and here is the JSFiddle example 这是JSFiddle示例
Here is a jquery script built to work with bootstrap, and that swaps in navbar-fixed-top at 50px scroll. 这是一个构建为可与引导程序一起使用的jquery脚本,该脚本以50px滚动滚动到navbar-fixed-top中。
$(window).scroll(function () {
var height = $('body').height();
var scrollTop = $(window).scrollTop();
if(scrollTop>50){
$(".navbar-default").addClass("navbar-fixed-top");
} else {
$(".navbar-default").removeClass("navbar-fixed-top");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.