gpt4 book ai didi

javascript - 当滚动停止百分比时触发

转载 作者:行者123 更新时间:2023-11-28 19:41:02 25 4
gpt4 key购买 nike

当用户停止滚动并提醒一些消息时,我需要触发。但我需要按百分比触发它。

例如,如果用户滚动,但当他停止滚动以及滚动超过窗口的 80% 时,我想向用户发出一些警告消息。

我有这个代码,当 srcoll 停止时触发,并且不知道如何使用滚动百分比来实现此工作:

$.fn.scrollStopped = function(callback) {           
$(this).scroll(function(){
var self = this, $this = $(self);
if ($this.data('scrollTimeout')) {
clearTimeout($this.data('scrollTimeout'));
}
$this.data('scrollTimeout', setTimeout(callback,250,self));
});
};

jQuery(window).scrollStopped(function(){
alert('stopped');
});

最佳答案

可以计算文档和窗口的高度,然后与滚动条当前的垂直位置进行比较:

$.fn.scrollStopped = function(callback) {           
$(this).scroll(function(){
var self = this, $this = $(self);
if ($this.data('scrollTimeout')) {
clearTimeout($this.data('scrollTimeout'));
}
$this.data('scrollTimeout', setTimeout(callback,250,self));
});
};

jQuery(window).scrollStopped(function(){
console.log(jQuery(window).scrollTop());
if($(window).scrollTop() > ($(document).height() - $(window).height())*0.8){
alert('You have scrolled more than 80%');
}

});

Try it yourself 。您可能想阅读有关这两个函数的更多详细信息; .scrollTop().height() .

关于javascript - 当滚动停止百分比时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com