[英]Performance problems when user scrolls to bottom of the page
我有一个固定在页面右下方的按钮;
.btn-this{
width: 313px;
height: 61px;
background-color: #ebb762;
position: fixed;
bottom: 0;
right: 0;
z-index: 99;
}
所以我想在页面的某个位置滚动后将按钮的位置从“固定”更改为“相对”。
现在,起初我为我完成了这项工作:
JQuery(function ($) {
var masinfo = $(".btn-this");
$(window).scroll(function () {
var scroll = $(window).scrollTop();
if (scroll >= 457) {
masinfo.css('position', 'relative');
} else {
masinfo.css({
position: 'fixed',
bottom: '0'
});
}
});
});
但是后来我确实看到这不是一个高质量的解决方案,因为我将不得不粘贴并更改每个页面的代码,而且这将永远不会很顺利。
所以我确实检查了这两个帖子:
检查用户是否已滚动到底部
到达父容器底部的“ #sidebar”类时,如何添加“ bottom”类?
但是话又说回来,我无法使其顺利运行,代码可以正常工作,但是执行时间很多,
使用时:
if ($(window).scrollTop() + $(window).height() > $(document).height() - 78) {
console.log('bottom');
}
控制台记录“底部” 11次;
我试过使用去抖器,但是我只是不理解这个概念,或者只是没有正确地使用它。
有人能做这样的事吗?
防反跳功能限制了该功能可以触发的速率。 阅读有关反跳功能的更多信息还有另外一种方法可以改善requestAnimationFrame的动画/滚动性能
所以你的函数可以写成
JQuery(function ($) {
var masinfo = $(".btn-this");
$(window).scroll(function () {
// If there's a timer, cancel it
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Setup the new requestAnimationFrame()
timeout = window.requestAnimationFrame(function () {
if ($(window).scrollTop() + $(window).height() > $(document).height() - 78) {
console.log('bottom');
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.