1 頁 (共 1 頁)

动端浏览器取消右划后退 防止页面后退

文章發表於 : 2018-09-27 3:29 pm
ETERNAL
$(function () {
//防止畫面下滑刷新
const target = window;
let lastY = 0;

target.addEventListener('touchmove', handleTouchMove);

function handleTouchMove(e) {
const { pageY } = e.changedTouches[0];
const scrollY = target.pageYOffset || target.scrollTop || 0;
if (pageY > lastY && scrollY === 0) {
e.preventDefault();
}
lastY = pageY;
}

//防止页面后退
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
});