javascript - 向下滚动超过 x 像素时隐藏导航栏并在向上滚动时显示
问题描述
我想在向下滚动时隐藏我的导航栏并在向上滚动时显示,但是在窗口向下滚动 x 页面向下的像素量之后发生向下滚动。(在 #section1 之后的代码中)并且如果向上滚动立即显示导航栏。我正在使用此代码,但我不知道如何定义职位。
var scrollp=0;
(function ($) {
$(document).ready(function(){
$(function () {
$(window).scroll(function () {
// ask about the position of scroll
if ($(this).scrollTop() < scrollp) {
$('.navbar').fadeIn();
scrollp= $(this).scrollTop();
} else {
$('.navbar').fadeOut();
scrollp= $(this).scrollTop();
}
});
});
});
}(jQuery));
解决方案
只需检查 else 子句中的 scrollTop,如下所示:
... else {
scrollp = $(this).scrollTop();
if (scrollp > fadeOutAfter) // Set fadeOutAfter to wherever you want the navbar to fade out
$('.navbar').fadeOut();
}
推荐阅读
- c# - 开始使用soap web服务客户端c#
- jquery - jQuery select2 外部内容无法选择
- android - 面对这个 java.lang.IllegalStateException: Bundle length is notaligned by 4: 9 in Model class which implement Parcelable
- tsql - 为什么我的计数语句只返回所有列中的第一列值?
- javascript - 在两个文本字段之间切换自动对焦
- flutter - 如何使用 StreamTransform Flutter 进行取决于另一个字段的值的验证
- kubernetes - Kubernetes-dashboard empty : 禁止所有资源
- twitter-bootstrap - 为什么 bootstrap col 上的文本颜色是蓝色的?以及如何仅使用 Bootstrap css 重新设计我的页面
- jquery - 数据中带有 location.href 的 jquery ajax 帖子不起作用
- c# - 从复选框中选择多个项目并将现有 SQL Server 查询添加到 ASP.NET 中的 if 语句中