How to prevent a browser from going back/forward in history when scrolling horizontally?

前端 未结 5 1900
长发绾君心
长发绾君心 2020-12-09 17:39

How can one disable a modern browsers default functionality using JQuery or Native JS for going backwards or forwards when scrolling horizontally?

This usually happe

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 18:16

    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.

提交回复
热议问题