旧demo
$(".buy_btn").click(function(){ $(".pop").fadeIn(300); disable_scroll(); }); $("input[name=‘cancel‘]").click(function(){ $(".pop").fadeOut(300); enable_scroll(); }); var keys = [37, 38, 39, 40]; function preventDefault(e) { e = e || window.event; if (e.preventDefault) e.preventDefault(); e.returnValue = false; } function keydown(e) { for (var i = keys.length; i--;) { if (e.keyCode === keys[i]) { preventDefault(e); return; } } } function wheel(e) { preventDefault(e); } function disable_scroll() { if (window.addEventListener) { window.addEventListener(‘DOMMouseScroll‘, wheel, false); } window.onmousewheel = document.onmousewheel = wheel; document.onkeydown = keydown; } function enable_scroll() { if (window.removeEventListener) { window.removeEventListener(‘DOMMouseScroll‘, wheel, false); } window.onmousewheel = document.onmousewheel = document.onkeydown = null; }
新demo
$(".buy_btn").click(function(){ $(".pop").fadeIn(300); disableScroll(); }); $("input[name=‘cancel‘]").click(function(){ $(".pop").fadeOut(300); enableScroll() }); var keys = { 37: 1, 38: 1, 39: 1, 40: 1 }; function preventDefault(e) { e = e || window.event; if (e.preventDefault) e.preventDefault(); e.returnValue = false; } function preventDefaultForScrollKeys(e) { if (keys[e.keyCode]) { preventDefault(e); return false; } } var oldonwheel, oldonmousewheel1, oldonmousewheel2, oldontouchmove, oldonkeydown , isDisabled; function disableScroll() { if (window.addEventListener) // older FF window.addEventListener(‘DOMMouseScroll‘, preventDefault, false); oldonwheel = window.onwheel; window.onwheel = preventDefault; // modern standard oldonmousewheel1 = window.onmousewheel; window.onmousewheel = preventDefault; // older browsers, IE oldonmousewheel2 = document.onmousewheel; document.onmousewheel = preventDefault; // older browsers, IE oldontouchmove = window.ontouchmove; window.ontouchmove = preventDefault; // mobile oldonkeydown = document.onkeydown; document.onkeydown = preventDefaultForScrollKeys; isDisabled = true; } function enableScroll() { if (!isDisabled) return; if (window.removeEventListener) window.removeEventListener(‘DOMMouseScroll‘, preventDefault, false); window.onwheel = oldonwheel; // modern standard window.onmousewheel = oldonmousewheel1; // older browsers, IE document.onmousewheel = oldonmousewheel2; // older browsers, IE window.ontouchmove = oldontouchmove; // mobile document.onkeydown = oldonkeydown; isDisabled = false; } window.scrollHanlder = { disableScroll: disableScroll, enableScroll: enableScroll };
时间: 2024-10-13 13:54:21