jQuery實現在鼠標滾動後導航欄保持固定(吸頂效果)

內容說明

效果: 頁面中有導航欄,當頁面滾動超出一定範圍時,它會固定在設置好的位置,一般是固定在頂部。

在這裏插入圖片描述

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery實現在鼠標滾動後導航欄保持固定</title>
<style>
* {
    padding: 0;
    margin: 0
}
ul, li {
    list-style: none;
}
a {
    text-decoration: none;
    color: #333;
}
.content {
    width: 1200px;
    margin: auto;
}
.menu {
    line-height: 60px;
    height: 60px;
}
.menu li {
    float: left;
    padding: 0 15px;
}
.sticky {
    position: fixed;
    height: auto;
    z-index: 10000;
    background-color: #2778af;
    width: 100%;
    top: 0px;
    background-color: rgba(30, 30, 30, 0.94);
    border-bottom: none;
    padding: 0;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -ms-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}
.sticky a {
    color: #fff;
}
.con {
    min-height: 2000px;
}
</style>
<script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.js"></script> 
<script>
	$(window).scroll(function() {
        var wScroll = $(this).scrollTop();
        if (wScroll > 0) {
            $('.menu-box').addClass('sticky');
        } else {
            $('.menu-box').removeClass('sticky');
        }
    });	
</script>
</head>
<body>
<div class="menu-box">
  <div class="content">
    <ul class="menu">
      <li><a href="home">首頁</a></li>
      <li><a href="life">走進航大</a></li>
      <li><a href="direction">報考指南</a></li>
      <li><a href="college">學院介紹</a></li>
      <li><a href="type">招生類型</a></li>
    </ul>
  </div>
</div>
<div class="con"></div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章