gpt4 book ai didi

javascript - 向下滚动页面后固定标题跳转

转载 作者:行者123 更新时间:2023-11-28 17:38:47 25 4
gpt4 key购买 nike

我发现了几个类似的问题,但无法帮助我。当我向下滚动页面时,标题/导航栏不会随页面“平滑”移动。在我到达页面下方一定数量后,标题“跳转”,但之后就没问题了。我有以下固定 header 的代码:

$(window).scroll(function(){
if ($(window).scrollTop() >= 147) {
$("#top_nav").addClass("fixed");
$("#top_nav").css("position", "fixed");
$("#top_rule").hide();
$("#bottom_rule").hide();
}
else {
$("#top_nav").removeClass("fixed");
$("#top_nav").css("position", "initial");
$("#top_rule").show();
$("#bottom_rule").show();
}
});

我的 CSS:

.fixed {
width: 100%;
background: white;
border-bottom: 1px solid black;
box-shadow: 0 0 20px #000000;
top: 0px;
padding-top: 15px;
padding: 10px;
}

我的 CSS 中没有 position: fixed,因为某些原因它不起作用,所以我使用 jQuery 将位置设置为 fixed。

我已经在 jsfiddle.net 上发布了我页面的其余部分
http://jsfiddle.net/5n4pF/如果我没有解释推进力,请询问,我会尽力解释得更好:)
提前致谢

编辑当我达到147px的时候,它一定不能跳。看起来好像它“隐藏和显示”。相反,它必须在您向下滚动页面时平稳移动。

最佳答案

您应该将页眉定位为绝对位置。并给新闻一个 margin-top 标题的大小。您的 position: fixed 不起作用的原因是因为您将其固定在相对元素内。它固定在该元素内部(这不是您想要的,因为您希望它固定在页面顶部)。

它跳跃是因为您将元素从静态更改为固定。突然之间,您在布局中错过了大约 53 像素的高度。这让它跳跃。

在这个 fiddle 中:http://jsfiddle.net/5n4pF/3/

* {
margin: 0px;
padding: 0px;
}

header {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
}

#black_top {
background: black;
font-size: 5px;
display:block;
width: 100%;
height: 5px;
}

#logo_header {
margin-top: 20px;
}

.list_item {
list-style-type: none;
display: inline-block;
font: 16px Arial;
padding: 10px 30px 10px 30px;
text-align: center;
}

#top_nav {
font: Arial 30px;
display: block;
width: 100%;
}

.nav_links {
text-decoration: none;
color: black;
}

hr {
margin-right: 30px;
margin-left: 30px;
color: #f00;
opacity: 0.3;
}

.nav_bullets {
color: #D6D6D6;
}

::-moz-selection {
background: #93E6E5;
}

::selection {
background: #b3d4fc;
}

#news_block {
/*Chrome & Safari*/
-webkit-column-count: 3;
-webkit-column-gap: 40px;
/*Firefox*/
-moz-column-count:3;
-moz-column-gap: 40px;
margin: 20px;
position: absolute;
top: 249px;
width: 100%;
box-sizing: border-box;
}

#search_field {
font-size: 25px;
}

.fixed {
width: 100%;
box-sizing: border-box;
background: white;
border-bottom: 1px solid black;
box-shadow: 0 0 20px #000000;
top: 0;
position: fixed;
padding-top: 15px;
padding: 10px;
}

给出了正确的代码。它的宽度仍然有点问题。但总的来说代码不是很整洁。所以我会把那部分留给你。希望这对你有用。

关于javascript - 向下滚动页面后固定标题跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com