本文实例讲述了jquery实现页面滚动时顶部导航显示隐藏效果代码。分享给大家供大家参考。具体如下:
运行效果截图如下:
具体代码如下:
引入核心文件
构建html,margint这个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实现页面滚动时顶部导航显示隐藏的总体构思,希望大家沿着这个思路完成导航显示隐藏的效果,谢谢大家阅读。