- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 this method 构建了一个滚动到顶部按钮上this page .
我还编写了一个函数
,它可以根据 anchor 链接处理滚动到各部分的操作,以便各部分可以平滑地滚动到视口(viewport)中,同时考虑到导航在顶部占用的空间。
这是重现该问题的代码片段:
const navHeight = 42;
const $scrollTopBtn = $('#scroll-top-btn');
$(window).scroll(function() {
const scrollTop = $(document).scrollTop();
if (scrollTop > navHeight) {
$scrollTopBtn.fadeIn();
} else {
$scrollTopBtn.fadeOut();
}
});
$(document).ready(() => {
$('a[href*="#"]').on('click', function(event) {
const hash = this.hash;
if (this.hash) {
event.preventDefault();
$('html, body').animate({
scrollTop: $(hash).offset().top - navHeight,
}, 350);
window.location.hash = this.hash;
}
});
});
body {
margin: 0;
font-family: monospace;
}
nav {
position: sticky;
top: 0;
display: flex;
background: white;
border-bottom: 2px solid black;
}
a {
height: 40px;
display: flex;
align-items: center;
padding: 0 8px;
border-right: 2px solid black;
}
.block {
min-height: 100vh;
border-bottom: 2px solid black;
display: flex;
justify-content: center;
padding: 32px 50vw 32px 32px;
box-sizing: border-box;
}
#scroll-top-btn {
position: fixed;
right: 8px;
bottom: 8px;
width: 40px;
height: 40px;
border: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
background: white;
}
<nav>
<a href="#block1">Block 1</a>
<a href="#block2">Block 2</a>
<a href="#block3">Block 3</a>
</nav>
<div id="block1" class="block">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div id="block2" class="block">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</div>
<div id="block3" class="block">
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?
</div>
<button id="scroll-top-btn">👆</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
在实际站点上,该问题只是偶尔发生。单击左侧导航中的 anchor 链接时,滚动到顶部按钮会随着页面滚动而短暂闪烁。这种情况很难复制,并且遵循相同的步骤并不总是会发生,但在首次登陆常见问题解答页面并单击“疑难解答”链接时通常会发生这种情况。
如果我不运行 anchor 点击滚动功能
,按钮不会闪烁。
我不明白为什么按钮会闪烁或者它取决于什么,因为它只会在页面滚动超过导航的高度
(60px
)后淡出。
我还使用 smoothState.js
,它要求我在 $(document).ready
及其自身的 onAfter
上运行函数> 回调function
,但滚动按钮监听器function
仅在$(window).scroll
上运行。
什么可能导致按钮闪烁问题以及如何解决该问题?
最佳答案
发生的情况是,当您加载页面时,滚动位置将位于最顶部。然后,当您单击 anchor 并且平滑滚动代码开始缓慢滚动页面时,将调用滚动监听器,但滚动位置仍将低于 navHeight
,因此它将淡出(隐藏) #scoll-top-btn
按钮。
不久之后,滚动监听器将再次被调用,但这次滚动位置将高于 navHeight
,因此它将淡入(显示) #scoll-top-btn
按钮。
您可以通过检查滚动方向和滚动位置来解决此问题,以便#scoll-top-btn
按钮仅在向下滚动时显示或仅在向上滚动时隐藏。
另外,它看起来像你的 #scoll-top-btn
按钮最初是可见的,所以我会添加 style="display: none"
以便在页面加载时它始终隐藏(但这本身并不能解决您的问题)。
在这里您可以看到应用了这两个更改后它是如何工作的:
const navHeight = 42;
const $scrollTopBtn = $('#scroll-top-btn');
let lastScrollTop = 0;
$(window).scroll(function() {
const scrollTop = $(document).scrollTop();
const scrollUp = scrollTop < lastScrollTop;
lastScrollTop = scrollTop;
if (scrollTop > navHeight && !scrollUp) {
$scrollTopBtn.stop().fadeIn();
} else if (scrollTop < navHeight && scrollUp) {
$scrollTopBtn.stop().fadeOut();
}
});
$(document).ready(() => {
$('a[href*="#"]').on('click', function(event) {
const hash = this.hash;
if (this.hash) {
event.preventDefault();
$('html, body').animate({
scrollTop: $(hash).offset().top - navHeight,
}, 350);
window.location.hash = this.hash;
}
});
});
body {
margin: 0;
font-family: monospace;
}
nav {
position: sticky;
top: 0;
display: flex;
background: white;
border-bottom: 2px solid black;
}
a {
height: 40px;
display: flex;
align-items: center;
padding: 0 8px;
border-right: 2px solid black;
}
.block {
min-height: 100vh;
border-bottom: 2px solid black;
display: flex;
justify-content: center;
padding: 32px 50vw 32px 32px;
box-sizing: border-box;
}
#scroll-top-btn {
position: fixed;
right: 8px;
bottom: 8px;
width: 40px;
height: 40px;
border: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
background: white;
}
<nav>
<a href="#block1">Block 1</a>
<a href="#block2">Block 2</a>
<a href="#block3">Block 3</a>
</nav>
<div id="block1" class="block">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div id="block2" class="block">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</div>
<div id="block3" class="block">
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?
</div>
<button id="scroll-top-btn" style="display: none">👆</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
关于javascript - anchor 滚动功能干扰滚动事件监听器逻辑以隐藏/显示 "back to top"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61721191/
这是我的 + 部分: function insert(){ var linkElement = document.getElementById("BackButton"); var linkEl
我有两辆 buggy 在轨道上移动,它们都使用超声波测量模块来检测路径中的障碍物,并由 Arduino 微 Controller 控制。两个超声波传感器以相同的频率工作,并且这个频率不能改变。两个超声
希望能快速解决这个问题。我有一个用文件位置替换特定值的脚本。不幸的是,该位置似乎经常包含\n 或 n\(因为当前目录位于临时文件夹中),导致该行中断或从该行中完全删除自身,从而使文件夹位置无效。 临时
我使用的是 Mac OSX 10.10.1。我通常使用brew或pip来安装所有东西。我注意到我的 virtualenv 总是抓取我本地安装的所有 Python 包。我通过删除我的 $PYTHONPA
我正在尝试使用 str_replace 将两个字母的语言缩写转换为完整的单词。我遇到的问题是它们在回显时会相互影响。 $lang = str_replace("en", "English", $lan
我已经创建了用于结束通话的 aidl 文件 ITelephony.aidl。现在,在通话结束之前,我需要生成正在进行的通话中的语音,以便听众认为接下来的工作有问题。我尝试通过三种方式自己解决这个问题:
使用这个查询,我得到了我需要的每个客户下的订单数量的结果。但是,当我在 SELECT 字段中使用 SUM(OrderLine.ActualPrice) 并使用内部联接将 Order 表链接到包含价格的
在 Spring Boot 应用程序中,仅使用注释,我想实现安全性 我已将 @EnableGlobalMethodSecurity(jsr360Enabled=true) 添加到配置类。该类还有一个
我一直在 Linux 下开发一个多线程程序,对信号处理有特殊要求。例如,我需要程序打印单个 Ctrl-C 输入的统计信息。 我使用 pthread_sigmask 和 sigwait 实现了这一点,即
作为我第一次涉足 Dynamic Scala 领域,我想我会尝试通过 applyDynamic 访问 bean 属性。 我的第一个非常粗略的剪裁是 trait BeanProperties ext
我知道这是一个令人沮丧的话题,但我尝试遵循文档和其他类似的帖子。 我的问题是,除非重新加载页面,否则 JS 将无法工作。 我有以下 JS: $(function() { $('a.form-sho
我正在使用 JQuery Ajax 从服务器获取脚本。 $.ajax({ url: src, type: "GET", dataType: "script", time
我正在尝试根据每个产品的选定数量分别输出计算价格。我尝试复制代码并重命名所有变量,但输出是由各种增加/减少按钮触发的。 这是目前的代码: $(".incr-btn_mobile").on("click
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: jQuery in Greasemonkey 1.0 conflicts with websites usi
我在一个 iOS 应用程序 (Obj-C) 中有一个 View ,它在中心有一个 ImageView ,紧接着在其下方有一个 slider 。 ImageView 显示专辑封面, slider 可用于
我有一个 View Controller ,将 UITapGestureRecognizer 附加到它的主 UIView,并使用户能够点击屏幕以使状态、导航和工具栏重新出现/消失(如照片应用程序)。我
我正在尝试在我的应用程序中设置 log4j,但我的一个第三方 JAR 似乎以某种方式不断将我的日志级别设置为“关闭”。它仅在执行该库中的代码时发生。我不知道它是如何做到这一点的,因为我没有使用它知道名
我有一个 UIScrollView,其框架与包含的 View Controller 相同。 当 ScrollView 的缩放比例大于 1 时,从屏幕左侧 50% 开始从左向右滑动会导致 interac
我是网页设计的新手,最近接触了 Bootstrap。我最近也开始使用 Codepen。在 Codepen 中完成一个元素后,我将代码从那里复制并粘贴到 Sublime 中,无论出于何种原因,它更改了我
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我是一名优秀的程序员,十分优秀!