Can't prevent `touchmove` from scrolling window on iOS
I recently ran into this same problem. You'll need to pass { passive: false }
when registering the touchmove
event listener. e.g.
document.addEventListener('touchmove', function(e) {
e.preventDefault();
}, { passive: false });
This is because document touch event listeners are now passive by default in Safari 11.1, which is bundled with iOS 11.3. This change is documented in the Safari 11.1 release notes:
Web APIs
- [...]
- Updated root document touch event listeners to use passive mode improving scrolling performance and reducing crashes.
IOS Prevent touchmove event being catched by the body behind fixed element
There really is not a good solution for dealing with a fixed element that has scrolling inside of it on mobile browsers.
I have not tested other browsers besides Safari but I've learned that other browsers are not too fond of this combination either.
The best and most flexible solution is to make your full screen
elements absolute
positioned. This will fix common issues with swiping and positioning.
But what if my element is in a
relative
container?
Then you are out of luck and need to grab your element, remove it from the dom and place it up as high in the dom as you can when you are opening your fullscreen
element.
Afterwards you need to place your element back in it's original position. They best way I know of to do this, is to leave behind a placeholder for you to append/prepend to. The dom has no method your giving you the exact location of an element therefore if you don't want the order of elements to change you are forced to do this.
Feel free to leave comments or suggestions on this answer if you feel like improvements can be made.
How to disable scrolling on body in iOS 13 safari (when saved as PWA to the homescreen)?
I combined try 2 and try 4 from the question. The fixed body shows no overflow scrolling and the scroll reset prevents the long animation of the overflow scrolling in the background. It's really ugly but it kinda works.
body {
position: fixed;
}
window.addEventListener("scroll", (e) => {
e.preventDefault();
window.scrollTo(0, 0);
});
iOS Disable Page Scrolling with overflow-scrolling: touch
Try swapping around the logic in your window
and scrollable
element listeners like so:
// window or document
window.addEventListener("touchmove", function(event) {
if (!event.target.classList.contains('scrollable')) {
// no more scrolling
event.preventDefault();
}
}, false);
// No special listeners needed on .scrollable elements
This way, you only prevent default when trying to scroll non-scrollable elements.
You will still have a problem that at the top/bottom of the scrollable content starting a drag can cause the whole app to "bounce". To fix this problem, see Joe Lambert's ScrollFix.
Related Topics
How to Capture Picture with Avcapturesession in Swift
iOS Autolayout to Center My View Between Two Views
Uiwebview Random Crash at [Uiviewanimationstate Release]: Message Sent to Deallocated Instance
Spacing Between Uitableviewcells
Setting Maximum Number of Characters of 'Uitextview ' and 'Uitextfield '
Nslocalizedstring Only Retrieves the Key, Not the Value in Localizable.Strings (Ios)
Xcode: Could Not Inspect the Application Package
How to Get the Build Uuid in Runtime and the Image Base Address
How Secure Is Nsuserdefaults on iOS 8,9
How to Set Image in Circle in Swift
How to Get Current Longitude and Latitude Using Cllocationmanager-Swift
How to Apply a Tintcolor to a Uiimage
How to Downscale a Uiimage in iOS by the Data Size
Swift - Instantiating a Navigation Controller Without Storyboards in App Delegate
What Do Horizontalaccuracy and Verticalaccuracy of a Cllocation Refer To