Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent swiping between web pages in iPad Safari

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?

like image 280
ᴇʟᴇvᴀтᴇ Avatar asked May 03 '18 13:05

ᴇʟᴇvᴀтᴇ


People also ask

How do I turn off swipe on Safari?

There isn't a Safari-specific setting for this. To disable it in Safari, you will need to disable the swiping gestures system-wide.

How do I turn off swipe on my iPad?

Answer: A: As stated in this article, Switch between apps on iPad - Apple Support. To turn off the multifinger swipe gesture, go to Settings > Home Screen & Dock > Multitasking.

How do I get my iPhone screen to stop swiping?

To turn off the swipe keyboard feature on your iPhone: Open “Settings” > “General”. Tap on “Keyboard”. Scroll down and toggle off “Slide to Type”.


2 Answers

In iOS 13.4+ you can now preventDefault on "touchstart"

Let's say we have a <div class="block-swipe-nav"> on the page that spans the entire width of the viewport and we want to prevent swipe navigation on that element.

const element = document.querySelector('.block-swipe-nav');

element.addEventListener('touchstart', (e) => {

    // is not near edge of view, exit
    if (e.pageX > 10 && e.pageX < window.innerWidth - 10) return;

    // prevent swipe to navigate gesture
    e.preventDefault();
});

I've written a short article on blocking swipe with some additional information.

like image 50
Rik Avatar answered Sep 19 '22 12:09

Rik


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.
  2. Abort gesture if, at any time, you receive an event with >1 touches.
  3. Continue gesture if you receive a touchmove event mostly in the x-direction.
  4. Abort gesture if you receive a touchmove event mostly the y-direction.
  5. End gesture if you receive a touchend event.

The full guide is poster here.

Let me know if you need more help.

Nitin, Defuzed

like image 23
Nitin K. Avatar answered Sep 21 '22 12:09

Nitin K.