[英]How can I change the position of my navbar to fixed after scrolling to the top?
I want to change my navbar position to fixed. 我想将导航栏位置更改为固定。
Here is my code: 这是我的代码:
$(document).ready(function() { $(document).scroll(function() { var scroll = $(this).scrollTop(); var topDist = $("#container").position(); if (scroll > topDist.top) { $('nav').css({ "position": "fixed", "top": "0" }); } else { $('nav').css({ "position": "static", "top": "auto" }); } }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <nav> <ul> <li><a href="#">LINK 1</a></li> <li><a href="#">LINK 2</a></li> <li><a href="#">LINK 3</a></li> <li><a href="#">LINK 4</a></li> </ul> </nav>
You can utilize the position: sticky
property which is well supported now on most browsers: 您可以利用
position: sticky
属性,目前大多数浏览器都支持该属性:
#container {
position: sticky;
top: 0;
}
https://jsfiddle.net/wqp9430L/ https://jsfiddle.net/wqp9430L/
Your JS isn't actually triggering because you're detecting scroll
on the $(document)
but your document and window isn't actually scrolling because your content is actually scrolling within .parallax
. 您的JS实际上没有触发,因为您正在检测
$(document)
上的scroll
,但是您的文档和窗口实际上并未滚动,因为您的内容实际上是在.parallax
滚动。 So instead of detecting the scroll on document you have to detect scroll on parallax
: 因此,无需检测文档上的滚动,而必须检测
parallax
滚动:
$(document).ready(function() {
$('.parallax').scroll(function () {
var scroll = $(this).scrollTop();
var topDist = $("#container").offset();
if (scroll > topDist.top) {
$('nav').css({"position":"fixed","top":"0"});
} else {
$('nav').css({"position":"static","top":"auto"});
}
});
});
Now while this works in terms of getting actually scroll values: https://jsfiddle.net/wqp9430L/2/ you'll notice there's a few issues. 现在,尽管这在获取实际滚动值方面起作用: https : //jsfiddle.net/wqp9430L/2/,您会注意到有一些问题。
As you scroll the offset changes, so you should move topDist
outside of the scroll
function to get the initial value, but 滚动偏移量更改时,因此应将
topDist
移动到scroll
功能之外以获取初始值,但是
If the fiddle loads too quickly (before you images finishes rendering) topDist
becomes in accurate, so you'll have to ensure topDist
is calculated after the images are fully rendered, then there's also 如果小提琴加载得太快(在图像完成渲染之前)
topDist
变得准确,那么您必须确保在完全渲染图像之后计算topDist
,然后还要
Changing your nav
to position: fixed
makes your nav fixed
to the body but since you're not scrolling in the body but rather in .parallax
you won't see the nav. 将
nav
更改为position: fixed
使导航fixed
在主体上,但是由于您不是在主体中滚动而是在.parallax
滚动,因此您不会看到导航。
For these reason you should use the CSS solution, but if you insist on using JS you solution should look more like: 由于这些原因,您应该使用CSS解决方案,但是如果您坚持使用JS,则解决方案应该更像:
if (imgLoaded) {
var topDist = $("#container").offset().top;
$('.parallax').scroll(function () {
var scroll = $(this).scrollTop();
if (scroll > topDist) {
$('nav').css({
position: 'fixed',
top: scroll + 'px'
});
} else {
$('nav').css({
position: 'static',
top: 'auto'
});
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.