jQuery实现在鼠标滚动后导航栏保持固定(吸顶效果)

内容说明

效果: 页面中有导航栏,当页面滚动超出一定范围时,它会固定在设置好的位置,一般是固定在顶部。

jQuery实现在鼠标滚动后导航栏保持固定(吸顶效果)_第1张图片


<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>

你可能感兴趣的:(html,js,前端)