如何防止移动铬的下拉刷新

Jae*_*Woo 23 javascript css mobile ios

我想防止移动铬的下拉到刷新(特别是iOS chrome).我的Web应用程序具有带设备宽度和设备高度视口的垂直平移事件,但是每当平移时,由于浏览器的默认功能,移动Chrome会自行刷新.此外,在Safari浏览器上,屏幕在平移事件期间滚动.我想禁用这些动作.

当然,我试过event.preventDefault(); 和触摸动作:无; 但它看起来不起作用.我应该在body标签上添加eventListner和touch-action吗?我期待有用的答案和例子.

Jos*_*Ott 23

更新:

对于最新版本的Chrome:

html,
body {
    overscroll-behavior-y: contain;
}
Run Code Online (Sandbox Code Playgroud)

旧解决方案:

由于默认情况下移动Chrome> = 56个事件侦听器是被动的,因此被动事件侦听器无法阻止默认设置. 看这里

您必须使用活动事件侦听器,如下所示:

document.addEventListener('touchstart', touchstartHandler, {passive: false});
document.addEventListener('touchmove', touchmoveHandler, {passive: false});
Run Code Online (Sandbox Code Playgroud)


小智 12

试试这个.

body {
  /* Disables pull-to-refresh but allows overscroll glow effects. */
  overscroll-behavior-y: contain;
}
Run Code Online (Sandbox Code Playgroud)

它对我很有用.由于其他javascript hacks,我有奇怪的滚动问题.阅读本文了解更多详情.

https://developers.google.com/web/updates/2017/11/overscroll-behavior

  • 仅适用于Chrome Android,不适用于Chrome IO (2认同)

Rub*_*ken 5

此处发布的仅 css 答案对我不起作用。我最终做了以下事情:

(function() {
    var touchStartHandler,
        touchMoveHandler,
        touchPoint;

    // Only needed for touch events on chrome.
    if ((window.chrome || navigator.userAgent.match("CriOS"))
        && "ontouchstart" in document.documentElement) {

        touchStartHandler = function() {
            // Only need to handle single-touch cases
            touchPoint = event.touches.length === 1 ? event.touches[0].clientY : null;
        };

        touchMoveHandler = function(event) {
            var newTouchPoint;

            // Only need to handle single-touch cases
            if (event.touches.length !== 1) {
                touchPoint = null;

                return;
            }

            // We only need to defaultPrevent when scrolling up
            newTouchPoint = event.touches[0].clientY;
            if (newTouchPoint > touchPoint) {
                event.preventDefault();
            }
            touchPoint = newTouchPoint;
        };

        document.addEventListener("touchstart", touchStartHandler, {
            passive: false
        });

        document.addEventListener("touchmove", touchMoveHandler, {
            passive: false
        });

    }
})();
Run Code Online (Sandbox Code Playgroud)


Mas*_*lah 5

滚动行为没有像这样对我有用。

body {
    overscroll-behavior: none 
}
Run Code Online (Sandbox Code Playgroud)