zoukankan      html  css  js  c++  java
  • 基于jQuery实现页面滚动时顶部导航显示隐藏效果

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
        <link rel="stylesheet" type="text/css" href="">
        <link rel="stylesheet" href="../css/bootstrap.min.css">
        <link rel="stylesheet" href="../css/common.css">
        <style type="text/css">
            .top-title {
                background:#e74c3c;
                color:white;
                font-size:24px;
                padding:5px;
                text-align:center;
                position: fixed;
                left:0;
                top:0;
                100%;
                transition: top .5s;
            }
            .hiddened {
                top: -90px;
            }
            .showed {
                top:0;
                z-index: 9999;
            }
            .margint {
                height: 1500px;
                background-color: red;
            }
    </style>
    </head>
    <body>
    <div class="top-title">这是顶部导航条</div>
    <div class="margint">
        <p>滚动看效果</p>
        <p>滚动看效果</p>
        <p>滚动看效果</p>
    </div>
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){  
            var winHeight = $(document).scrollTop();
            $(window).scroll(function() {
            var scrollY = $(document).scrollTop();// 获取垂直滚动的距离,即滚动了多少
            if (scrollY > 550){ //如果滚动距离大于550px则隐藏,否则删除隐藏类
            $('.top-title').addClass('hiddened');
        }else {
            $('.top-title').removeClass('hiddened');
        }  
        if (scrollY > winHeight){ //如果没滚动到顶部,删除显示类,否则添加显示类
            $('.top-title').removeClass('showed');
        } 
        else {
            $('.top-title').addClass('showed');
        }        
       });
    });
    </script>
    </body>
    </html>
    

      

  • 相关阅读:
    设置eclipse编码
    前端基础知识
    微信小程序
    jQuery下拉框
    Vue-cli的安装
    vue的数据交互形式
    node安装和小测试
    shui
    JQ-滚动条下拉无限的加载数据
    HTML-video全屏
  • 原文地址:https://www.cnblogs.com/candy-Yao/p/7407406.html
Copyright © 2011-2022 走看看