该
scroll事件无法取消。但是您可以通过 取消 以下交互事件来实现:
鼠标 和 触摸滚动 以及与滚动关联的 按钮 。
工作演示
// left: 37, up: 38, right: 39, down: 40,// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36var keys = {37: 1, 38: 1, 39: 1, 40: 1};function preventDefault(e) { e.preventDefault();}function preventDefaultForScrollKeys(e) { if (keys[e.keyCode]) { preventDefault(e); return false; }}// modern Chrome requires { passive: false } when adding eventvar supportsPassive = false;try { window.addEventListener("test", null, Object.defineProperty({}, 'passive', { get: function () { supportsPassive = true; } }));} catch(e) {}var wheelOpt = supportsPassive ? { passive: false } : false;var wheelEvent = 'onwheel' in document.createElement('div') ? 'wheel' : 'mousewheel';// call this to Disablefunction disableScroll() { window.addEventListener('DOMMouseScroll', preventDefault, false); // older FF window.addEventListener(wheelEvent, preventDefault, wheelOpt); // modern desktop window.addEventListener('touchmove', preventDefault, wheelOpt); // mobile window.addEventListener('keydown', preventDefaultForScrollKeys, false);}// call this to Enablefunction enableScroll() { window.removeEventListener('DOMMouseScroll', preventDefault, false); window.removeEventListener(wheelEvent, preventDefault, wheelOpt); window.removeEventListener('touchmove', preventDefault, wheelOpt); window.removeEventListener('keydown', preventDefaultForScrollKeys, false);}
更新:修复了带有被动监听器的Chrome台式机和现代移动浏览器
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)