在移动设备上,你可以"拉"出可滚动的内容,它会自动移动一段时间.如何检测这种运动何时停止? ontouchmove事件在内容自行移动时不会被触发.

这里是一个例子,我需要删除"更多内容"横幅时,用户已向左滚动到最后. 我正在阅读使用ontouchmove事件留下多少可滚动区域.但由于该事件仅在用户实际正在主动touch 移动内容时触发,因此无法读取还剩下多少可滚动区域.

如果我在按下内容的同时滚动到最后,一切都很好:

enter image description here

如果我快速"拉"它,并且它自己不停地滚动,则在到达结尾处时onouch移动事件不会触发,因此不可能检测到到达结尾处:

enter image description here

function onTouchMove() {
  const node = document.getElementById("scroll-container");
  const overflownOnRight = Math.ceil(node.scrollLeft + node.offsetWidth) < node.scrollWidth;
  if (!overflownOnRight) {
    document.getElementById("info-banner").style.display = "none";
  } else {
    document.getElementById("info-banner").style.display = "block";
  }
}
.scroll-container {
  margin-top: 100px;
  width: 100%;
  height: 40px;
  overflow-y: auto;

  &::-webkit-scrollbar {
    height: 0;
    width: 0;
    display: none;
  }
}

.reference-point {
  height: 30px;
  width: 100%;
  border: 5px dotted pink;
}

.scrollable {
  width: 1200px;
  height: 100%;
  background: blue;
  display: flex;
}
<div id="scroll-container" ontouchmove="onTouchMove(event)" class="scroll-container shadowed">
  <div class="scrollable">
    <div class="reference-point">
    </div>
  </div>
</div>

<div id="info-banner">
  Scroll Right For More Content
</div>

推荐答案

您可以使用一些浏览器支持的javascript Event scrollend,但这是一个相当新的功能.这将检测滚动何时停止,如herehere所示.Edit:在 comments 中提到,你可以在还不支持scrollend的浏览器上使用scrollend polyfill.

一种不太依赖浏览器支持的方法是不断判断scroll事件是否发生.

要使用scroll事件,您必须使用debounce 功能,因为当用户滚动时,滚动事件实际发生的频率.

然后,在go 反跳功能中,您可以判断用户是否已到达可滚动容器的末尾.

下面是一个例子:

function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

function handleScroll() {
    const node = document.getElementById("scroll-container");
    const overflownOnRight = Math.ceil(node.scrollLeft + node.offsetWidth) < node.scrollWidth;

    if (!overflownOnRight) {
        document.getElementById("info-banner").style.display = "none";
    } else {
        document.getElementById("info-banner").style.display = "block";
    }
}

var debouncedHandleScroll = debounce(handleScroll, 250);

document.getElementById("scroll-container").addEventListener('scroll', debouncedHandleScroll);

Javascript相关问答推荐

单击树元素时阻止焦点事件触发?

如何使用JavaScript向html元素添加样式

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

在JavaScript中检索一些文本

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

确定MutationRecord中removedNodes的索引

使用AJX发送表单后,$_Post看起来为空

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

调用removeEvents不起作用

Rehype将hashtag呈现为URL

如何在Angular拖放组件中同步数组?

将数组扩展到对象中

搜索功能不是在分页的每一页上进行搜索

向数组中的对象添加键而不改变原始变量

OnClick更改Json数组JSX中的图像源

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

为什么NULL不能在构造函数的.Prototype中工作

在JavaScript中将Base64转换为JSON

在没有任何悬停或其他触发的情况下连续交换图像