gpt4 book ai didi

javascript - float 菜单,如何将菜单固定在顶部?

转载 作者:行者123 更新时间:2023-11-28 02:44:50 25 4
gpt4 key购买 nike

我对 js 比较陌生,并且与 float 菜单作斗争。

这是我的 js 代码的样子

$(function(){
console.log('jest');
$(window).scroll(function(event){
console.log($('#menu').offset().top, $(this).scrollTop());
if ($('#menu').offset().top <= $(this).scrollTop()+$(window).height()) {
$('#menu').addClass("fixed");
} else {
$('#menu').removeClass("fixed");
}
});
});

当达到顶部时,fixed 类被正确添加。

我的问题是:我应该在 fixed 类中做什么才能使菜单固定在顶部?

最佳答案

不错的简单“位置:粘性”..

brand {
display: block;
background-color: pink;
padding: 10px;
font-size: 20pt;
}

header {
background-color: yellow;
border: 2px solid black;
padding: 10px;
position: sticky;
top: 0;
}

section {
background-color: silver;
padding: 10px;
}

body {
padding: 0;
margin: 0;
}
<brand>
<div>This is our branding,. It can scroll away.</div>
<small>for all your header needs,.. </small>
</brand>

<header>
This is the header
</header>
<section>
Our othe stuff can go in here.<br><br><br><br><br><br><br><br><br><br>
Scroll down<br><br><br><br><br><br>
Even further<br><br><br><br><br>
A little bit more<br><br><br><br><br>
Ok I'm bored now.
</section>

<header>
This is repeated, see how the header takes over
</header>
<section>
Our othe stuff can go in here.<br><br><br><br><br><br><br><br><br><br>
Scroll down<br><br><br><br><br><br>
Even further<br><br><br><br><br>
A little bit more<br><br><br><br><br>
Ok I'm bored now.
</section>

关于javascript - float 菜单,如何将菜单固定在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977094/

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