Javascript 滚动停止百分比时触发
我需要触发当用户停止滚动,并提醒一些消息。但我需要按百分比触发它 例如,我想提醒用户一些消息,如果他滚动,但当他停止滚动,如果他滚动超过80%的窗口 我有一段代码,当srcoll停止时触发,但不知道如何使用scroll percen完成此工作:Javascript 滚动停止百分比时触发,javascript,jquery,Javascript,Jquery,我需要触发当用户停止滚动,并提醒一些消息。但我需要按百分比触发它 例如,我想提醒用户一些消息,如果他滚动,但当他停止滚动,如果他滚动超过80%的窗口 我有一段代码,当srcoll停止时触发,但不知道如何使用scroll percen完成此工作: $.fn.scrollStopped = function(callback) { $(this).scroll(function(){ var self = this, $this = $(self);
$.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%');
}
});
。您可能想阅读有关这两个函数的更多详细信息;和。您可以计算文档和窗口的高度,然后将其与滚动条的当前垂直位置进行比较:
$.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%');
}
});
。您可能想阅读有关这两个函数的更多详细信息;和。您可以计算文档和窗口的高度,然后将其与滚动条的当前垂直位置进行比较:
$.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%');
}
});
。您可能想阅读有关这两个函数的更多详细信息;和。您可以计算文档和窗口的高度,然后将其与滚动条的当前垂直位置进行比较:
$.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%');
}
});
。您可能想阅读有关这两个函数的更多详细信息;和。我猜您必须计算到窗口高度的滚动距离我猜您必须计算到窗口高度的滚动距离我猜您必须计算到窗口高度的滚动距离我猜您必须计算到窗口高度的滚动距离我猜您必须计算到窗口高度的滚动距离