Prevent swiping between web pages in iPad Safari

2019-04-08 17:55发布

问题:

Swiping from the left and right edges of my iPad's Safari browser, moves between the currently open web pages. Is there any way to prevent it?

I have tried to add touchstart and touchmove handlers on the edge of the page that stopPropagation and preventDefault, but they seem to have no effect, nor does touch-action CSS.

A similar question was asked in 2014 with replies to the negative: iOS 7 - is there a way to disable the swipe back and forward functionality in Safari?

Is there a workround now in 2018?

回答1:

Apple provided these guidelines after iOS9.

The guide lets you disable

  1. Scrolling

    function touchMove(event) {

    // Prevent scrolling on this element
    
    event.preventDefault();
    
    ... 
    

    }

  2. Pinch and Zoom

    function gestureChange(event) {

    // Disable browser zoom
    
    event.preventDefault();
    
    ...
    

    }

You can identify a swipe gesture as follows:

  1. Begin gesture if you receive a touchstart event containing one target touch.
    1. Abort gesture if, at any time, you receive an event with >1 touches.
    2. Continue gesture if you receive a touchmove event mostly in the x-direction.
    3. Abort gesture if you receive a touchmove event mostly the y-direction.
    4. End gesture if you receive a touchend event.

The full guide is poster here.

Let me know if you need more help.

Nitin, Defuzed



回答2:

There appears to be no way to disable this functionality, so as a workround I've found that a deadzone of 24px on either side of the page seems to be enough to stop unintentional navigation.

Here is my CSS:

body {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 24px;
  right: 24px;
  background-color: #ccc;
}

Making the body position: fixed also stops Safari doing the annoying overscroll/bounce effect.



回答3:

Try this in the <body> tag:

onload='ontouchmove()="return(function(event) { event.preventDefault(); event.stopPropagation(); return(false); } );"'

I imagine there might be some side effects by not letting IOS know about moving but for SPW they're probably slight.



回答4:

You can't, it's been a problem since the early days of iOS. They repeatedly drag their feet on powerful web app features, such as service workers and webgl.

The best you can do is what you should do for every browser, make the best experience you can using feature detection for every browser. Long gone are the days of making all sites look the same on every browser.

Use a sandwich if needed, allow side swipes on browsers that support it. There is no shame in disabling a small feature on a few browsers for the benefit of the rest of your users.