gpt4 book ai didi

javascript - 我怎么能有位置 :fixed after a scroll event?

转载 作者:行者123 更新时间:2023-11-28 10:23:49 26 4
gpt4 key购买 nike

帮帮我。我想让我的导航菜单在向下滚动 500 像素后转到顶部的固定位置。如果有人能给我我需要的 java 脚本,我将不胜感激。提前致谢:)

如果有帮助的话,这是代码

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/png" href="img/favicon.png" />
<title></title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<style>
body {
margin: 0;
font-family: Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif;
padding: 0;
width: 100%;
font-size: 18px;

}

a {
text-decoration: none;
}

h1 {
}

h2 {
}

hr {
border: 0;
height: 1px;
background: #333;
background-image: -webkit-linear-gradient(left, #ccc, #333, #ccc);
background-image: -moz-linear-gradient(left, #ccc, #333, #ccc);
background-image: -ms-linear-gradient(left, #ccc, #333, #ccc);
background-image: -o-linear-gradient(left, #ccc, #333, #ccc);
}

/* ==============================Header================= */

.cover {
background: url(http://oi62.tinypic.com/a2ac8n.jpg) top center no-repeat;
background-size: cover;
width: 100%;
height: 500px;
margin-bottom: -400px;
position:fixed;

}

.menu,
.menu ul,
.menu li,
.menu a {
padding: 0;
border: none;
outline: none;
}

.menu {
text-shadow:0px 0px 10px #fff;
font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
margin: 400px 0px 0px 0px;
position: absolute;
width: 100%;
height: 100px;
-webkit-box-shadow: 0px 0px 20px 1px #666;
-moz-box-shadow: 0px 0px 20px 1px #666;
box-shadow: 0px 0px 20px 1px #666;
background: #FFF;
}

.menuWrap {
width: 1000px;
margin: auto;
}

.menu li {
list-style:none;
float: left;
width: 25%;
text-align: center;
}

.menu li a {
display: block;
font-weight: bold;
color: #000;
font-size: 20px;
line-height: 100px;
}

.menu li:hover > a,
.menu .active {
background-color:#09D5D5;
color: #FFF;
-webkit-transition: ease-in .2s;
-moz-transition: ease-in .2s;
-o-transition: ease-in .2s;
-ms-transition: ease-in .2s;
transition: ease-in .2s;
}

/* ========================Content======================== */

.mainContent{
background: #F8F8F8;
width: 100%;
position: absolute;
margin-top:500px;
}

.contentWrapper {
margin: auto;
width: 1000px;
padding-top: 50px;
}

.content {
margin-bottom: 50px;
padding: 40px;
border: #999 1px solid;
line-height: 25px;
color: #4D4D4D;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
background-color: #FFF;
box-shadow: 0px 0px 1px 1px #e1e1e1 inset, 0px 23px 30px -33px #4D4D4D;
}

</style>
<body class="body">
<div class="cover"></div>
<header class="mainHeader">
<nav>
<ul class="menu"><div class="menuWrap">
<li><a href="#" class="active">Page1</a></li>
<li><a href="#">Page2</a></li>
<li><a href="#">Page3</a></li>
<li><a href="#">Page4</a></li>
</div></ul>
</nav>
</header>

<div class="mainContent">
<div class="contentWrapper">
<article class="content">

</article>
<article class="content">

</article>
<article class="content">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</article>
</div>
<div>
</html>

最佳答案

使用额外的类:

.menu.fixed {
position:fixed;
top: 0;
margin: 0;
z-index: 3;
}

以及以下用于检测滚动的 jQuery 代码:

$(document).scroll(function()
{
if($(document).scrollTop()>=500)
$('.menu').addClass('fixed');
else
$('.menu').removeClass('fixed');
});

See working JSFiddle Demo

关于javascript - 我怎么能有位置 :fixed after a scroll event?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849012/

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