<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="jquery-1.11.3.min.js"></script>
<title>导航栏吸顶</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
.fixed {
position: fixed;
z-index: 1;
animation: reback .4s ease-in forwards;
}
@keyframes reback {
100% {
top: 0;
height: 70px;
background:red;
}
}
body {
color: #fff;
font-family: 'open-sans-bold', AvenirNext-Medium, sans-serif;
font-size: 18px;
text-align: center;
}
nav {
position: absolute;
top: 30%;
width: 100%;
height: 100px;
background: #fff;
}
nav li {
display: inline-block;
padding: 20px;
}
nav li a {
color: #999;
}
#header {
background: pink;
height: 500px;
}
#header div {
height:140px;
background: #cccccc
}
.content{
height: 1000px;
}
</style>
</head>
<body>
<div>
<section id="header">
<div></div>
<nav ref="nav">
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</nav>
</section>
<section class="content"></section>
</div>
<script>
$(function(){
$(window).scroll(function() {
if($(document).scrollTop()>70){
$('nav').addClass('fixed')
}else{
$('nav').removeClass('fixed')
}
});
})
</script>
</body>
</html>