Welcome 微信登录

首页 / 脚本样式 / JavaScript / 基于jquery实现页面滚动时顶部导航显示隐藏

本文实例讲述了jquery实现页面滚动时顶部导航显示隐藏效果代码。分享给大家供大家参考。具体如下:
运行效果截图如下:


具体代码如下:
引入核心文件

<script src="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;width: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");}});});
以上就是基于jquery实现页面滚动时顶部导航显示隐藏的总体构思,希望大家沿着这个思路完成导航显示隐藏的效果,谢谢大家阅读。