How can one disable a modern browsers default functionality using JQuery or Native JS for going backwards or forwards when scrolling horizontally?
This usually happens when using a trackpad and when scrolled to the end or start of a scrollable div.
How can one disable a modern browsers default functionality using JQuery or Native JS for going backwards or forwards when scrolling horizontally?
This usually happens when using a trackpad and when scrolled to the end or start of a scrollable div.
history.pushState(null, null, location.href);
window.onpopstate = function(event) {
history.go(1);
};
Demo: http://jsfiddle.net/DerekL/RgDBQ/show/
You won't be able to go back to the previous webpage, unless you spam the back button or hold down the back button and choose the previous entry.
Note: onpopstate
(or event onbeforeunload
) doesn't seem to work on iOS.
So I figured since i've created a web app, why not prompt the user for any unsaved changes which will defer the user from loosing any unsaved changes or ending up on the previous or next page in the browser history.
Here is the solution if any one else comes across this problem like I did:
window.onbeforeunload = function(e) {
return 'Ask user a page leaving question here';
};
If you're on a mac this is caused by the Track Pad Gestures.
System Preferences -> Trackpad -> More Gestures.
Not the JS solution you're looking for, but if you just want to prevent it for yourself you can turn the feature off.
Works in Chrome and Safari (i.e tested with Chrome and Safari):
// el === element on which horizontal scrolling is done
// (can be container of scrolled element or body or any other ancestor)
var preventHistoryBack = function (e) {
var delta = e.deltaX || e.wheelDeltaX;
if (! delta) {
return;
}
window.WebKitMediaKeyError /*detect safari*/ && (delta *= -1);
if (((el.scrollLeft + el.offsetWidth) === el.scrollWidth && delta > 0) || (el.scrollLeft === 0 && delta < 0) ) {
e.preventDefault();
}
};
el.addEventListener('mousewheel', preventHistoryBack);
Here's something that may (or may not) work for you using CSS' overscroll-behavior-x property. The linked MDN article goes into more detail, has usage examples, and browser support information.
Experiment with either the value contain
or none
. Quoting from the MDN link,
contain
: Default scroll overflow behavior is observed inside the element this value is set on (e.g. "bounce" effects or refreshes), but no scroll chaining occurs to neighboring scrolling areas, e.g. underlying elements will not scroll.
none
: No scroll chaining occurs to neighboring scrolling areas, and default scroll overflow behavior is prevented.
My guess is that whether history navigation falls under the category of "default scroll behaviour" is up to the particular browser / user-agent. The safest bet is probably to use none
, unless there is some other "default scroll behaviour" that you need to retain.
document.body.style.overscrollBehaviorX = "none";
body {
overscroll-behavior-x: none;
}
Check the browser support table in the MDN link, and test on the browsers and devices you want to support.