停止滚动后如何获取滚动位置

How to get scroll position after stop scroll?

本文关键字:滚动 获取 位置 何获取      更新时间:2023-09-26
var scrollVal;
  $(".book-body").scroll(function(){
  scrollVal = $(this).scrollTop();
  console.log(scrollVal);
});

以上是我的代码,这将console.log滚动时的滚动位置。然而,数字一直显示在控制台上。

我能做些什么,使它只显示一个位置数据后,我停止滚动?

谢谢。

如果我理解正确的话,由于您在滚动函数中写入了console.log(),因此滚动时它会明显显示数字。如果您需要在停止滚动时获得滚动位置,则可以使用scrollstop()。试试这样做:

$(".book-body").on("scrollstop",function(){
    console.log($(this).scrollTop());
});

不确定这是否能回答你的问题,但也许这个接近。

我一直在用它在我的一个项目

$(".book-body").scroll(function() {
  clearTimeout($.data(this, 'scrollTimer'));
  $.data(this, 'scrollTimer', setTimeout(function() {
    console.log("Haven't scrolled in 1000ms!");
  }, 1000));
});

它将在滚动停止后15秒触发一个函数,但您可以自定义。