• jQuery实现页面滚动时顶部动态显示隐藏


    很多时候你会看到页面上有一个始终固定在网页顶部的导航菜单,当页面向下滚动的时候,导航菜单动态隐藏,页面滚动到顶部时,导航菜单动态显示,淘宝也采用过此效果,很不错,当然,里面使用了CSS3动画效果,不支持低版本浏览器。代码相当的简单,只需几行就搞定。

    引入核心文件

    <script src="http://libs.useso.com/js/jquery/1.11.1/jquery.min.js"></script>

    构建html,margint这个div中为了出现滚动条而建,并无实际作用。

    <div class="top-title">这是顶部导航条</div>
    <div class="margint"><p>滚动看效果</p><p>滚动看效果</p></div>

    写入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;}

    top-title中定义了transition: top .5s;是指.5S时间内动画展示top方向数值的改为。如添加hidden类后,top-title会在0.5s内从top的0动画缓冲到-90PX。

    写入JS

    $(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');
            }               
     
         });
    });
  • 相关阅读:
    使用redis配置分布式session
    邮件发送整合
    Spark基础-scala学习(八、隐式转换与隐式参数)
    QMQ去哪儿网-mq中间件(启动失败)
    Spark基础-scala学习(七、类型参数)
    JMH实践-代码性能测试工具
    Spark基础-scala学习(五、集合)
    [JavaWeb基础] 012.Struts2 自定义标签使用
    html5学习之路_003
    [Objective-C] 017_UI篇_UIView(中)
  • 原文地址:https://www.cnblogs.com/dalulu/p/9284640.html
Copyright © 2020-2023  润新知