Home  >  Article  >  Web Front-end  >  Use jQuery to achieve fixed navigation bar effect

Use jQuery to achieve fixed navigation bar effect

一个新手
一个新手Original
2017-09-18 09:08:432272browse

jQuery implements fixed navigation bar effect:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>固定导航栏效果</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
body{
text-align: center;
}
.fixed{
top: 0;
left: 0;
position: fixed;
}
</style>
<script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
<script>
$(document).ready(function(){
//
 
入口函数
$(window).scroll(function(){
//
 
添加滚动事件
if($(window).scrollTop() >= $(".top").height()){
//
 
判断窗口的滚动如果大于top的高度就为nav添加fixed
$(".nav").addClass("fixed");
}else{
//
 
否则就为nav移除fixed
$(".nav").removeClass("fixed");
}
});
});
</script>
</head>
<body>
<p class="top">
<img src="img/top.png"/>
</p>
<p class="nav">
<img src="img/nav.png"/>
</p>
<p class="main">
<img src="img/main.png"/>
</p>
</body>
</html>


The above is the detailed content of Use jQuery to achieve fixed navigation bar effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn