本文实例讲述了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>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/950">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680003823895.png" alt="ViiTor实时翻译">
</a>
<div class="aritcle_card_info">
<a href="/ai/950">ViiTor实时翻译</a>
<p>AI实时多语言翻译专家!强大的语音识别、AR翻译功能。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="ViiTor实时翻译">
<span>116</span>
</div>
</div>
<a href="/ai/950" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="ViiTor实时翻译">
</a>
</div>
<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实现页面滚动时顶部导航显示隐藏的总体构思,希望大家沿着这个思路完成导航显示隐藏的效果,谢谢大家阅读。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号