当我向下滚动站点时,顶部显示黑色菜单栏,看起来像 float 栏。 但我认为这与jquery有关。我已经尝试过 CSS,但似乎并不像我想要的那样适合我
#menucontainer {
position:fixed;
top:0;
height: 250px
}
#firstElement {
margin-top: 250px
}
这是我希望菜单的网站基本概念:
最佳答案
用 ID 或类将您的菜单包装在一个 div 或部分中。
#yourID.fixed {
position: fixed;
top: 0;
left: 0;
z-index: 1;
width: 100%;
border-bottom: 5px solid #ffffff;
}
//STICKY NAV
$(document).ready(function () {
var top = $('#yourID').offset().top - parseFloat($('#yourID').css('marginTop').replace(/auto/, 100));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('#yourID').addClass('fixed');
} else {
// otherwise remove it
$('#yourID').removeClass('fixed');
}
});
});
不记得我从哪里得到这个,所以没有向我致敬,但它对我有用。
关于jquery - 向下滚动时如何构建 float 菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165915/