导航条案例

编辑本博客

  • 获取所需元素对象
  • 监听对象
  • 定位属性
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>导航条案例</title>
    <script src="js/jquery-3.3.1.js"></script>
    <style type="text/css">
        .nav{
            width: 400px;
            background-color: #FFE4C4;
            margin: 0 auto;
            position: fixed;
            top: 0px;
            display: none;
        }
        .content{
            width: 100%;
            height: 2000px;
        }
    </style>
</head>
<body>
    <div id="nav" class="nav">搜索框</div>
    <div id="head" style="width: 100%; height: 40px; background-color: #A9A9A9"></div>
    <div class="content"></div>
</body>
<script type="text/javascript">
    var h=$("#head").height()
    $(document).scroll(function () {
        var scorllTop=$(document).scrollTop();
        if(h<scorllTop){
            console.log("ok")
            $("#nav").css({display:"block"})
        }else {
            $("#nav").css({display:"none"})
        }
    })
</script>
</html>
View Code

猜你喜欢

转载自www.cnblogs.com/yaya625202/p/9202046.html