简体   繁体   English

滑动时禁用网页导航(后退和前进)

[英]Disable web page navigation on swipe(back and forward)

On a Windows phone, in IE users can go back and forward by swiping on the screen if the swipe is coming from the edge.在 Windows 手机上,在 IE 中,如果滑动来自边缘,则用户可以通过在屏幕上滑动来前后移动。 This OS level functionality is hampering my webpage's UX.这个操作系统级别的功能阻碍了我网页的用户体验。

Is there any js or css which can disable that?是否有任何 js 或 css 可以禁用它? Some hack would also do.一些黑客也可以。

A snapshot from windowsphone's website:来自 windowsphone 网站的快照:在此处输入图片说明

Here is the link to the reference page: http://www.windowsphone.com/en-in/how-to/wp8/basics/gestures-swipe-pan-and-stretch这是参考页面的链接: http : //www.windowsphone.com/en-in/how-to/wp8/basics/gestures-swipe-pan-and-stretch

Please note that I still need touchaction enabled for horizontal scrolling.请注意,我仍然需要为水平滚动启用触摸操作。

You Should Try this solution in two way :您应该以两种方式尝试此解决方案:

1) CSS only fix for Chrome/Firefox 1) CSS 仅适用于 Chrome/Firefox

 html, body {
    overscroll-behavior-x: none;
}

2) JavaScript fix for Safari 2) Safari 的 JavaScript 修复

if (window.safari) {
  history.pushState(null, null, location.href);
  window.onpopstate = function(event) {
      history.go(1);
  };
}

Over time, Safari will implement overscroll-behavior-x and we'll be able to remove the JS hack随着时间的推移,Safari 将实现overscroll-behavior-x ,我们将能够删除 JS hack

Possible duplicate of iOS 7 - is there a way to disable the swipe back and forward functionality in Safari? iOS 7 的可能副本- 有没有办法禁用 Safari 中的前后滑动功能?

Copy and paste this JavaScript:复制并粘贴此 JavaScript:

var xPos = null;
var yPos = null;
window.addEventListener( "touchmove", function ( event ) {
    var touch = event.originalEvent.touches[ 0 ];
    oldX = xPos;
    oldY = yPos;
    xPos = touch.pageX;
    yPos = touch.pageY;
    if ( oldX == null && oldY == null ) {
        return false;
    }
    else {
        if ( Math.abs( oldX-xPos ) > Math.abs( oldY-yPos ) ) {
            event.preventDefault();
            return false;
        }
    }
} );

If you want it minified, copy and paste this:如果你想缩小它,复制并粘贴这个:

var xPos=null;var yPos=null;window.addEventListener("touchmove",function(event){var touch=event.originalEvent.touches[0];oldX=xPos;oldY=yPos;xPos=touch.pageX;yPos=touch.pageY;if(oldX==null && oldY==null){return false;}else{if(Math.abs(oldX-xPos)>Math.abs(oldY-yPos)){event.preventDefault();return false;}}});

This is also a problem for Mac users who have configured swipe-left to navigate backwards.对于配置向左滑动向后导航的 Mac 用户来说,这也是一个问题。 You can't disable this setting, but you can prompt the user to confirm that they intended to navigate away https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload .您无法禁用此设置,但您可以提示用户确认他们打算离开https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

Check out this Codepen by David Hill.看看 David Hill 的这个 Codepen

var elem = document.getElementById("container"); //area we don't want touch drag

var defaultPrevent=function(e){e.preventDefault();}
elem.addEventListener("touchstart", defaultPrevent);
elem.addEventListener("touchmove" , defaultPrevent);

I actually think this is a cool way to build objects too.我实际上认为这也是构建对象的一种很酷的方式。

How about preventing the default action of the swipe event.如何防止滑动事件的默认操作。 Somewhere in your document.ready add (note I've included the document.ready in this example, only the function needs to be added):在你的 document.ready 某处添加(注意我在这个例子中包含了 document.ready,只需要添加函数):

$(document).ready(function(){ $(window).on('touchmove',function(e){e.preventDefault();}); });

In this case I believe the event is called 'touchmove'you may need to extend this to also ignore default behavior of touchstart/touchend but I'm not 100% sure.在这种情况下,我相信该事件称为“touchmove”,您可能需要扩展它以忽略 touchstart/touchend 的默认行为,但我不是 100% 确定。

*{
    -webkit-touch-callout: none;
}

The result is to completely deactivate any touch events结果是完全停用任何触摸事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM