如题。举个例子吧:写一个弹窗组件,弹窗未出现时页面可以滚动,弹窗出现后弹窗这一层的下面就不能滚动(但弹窗里的内容能滚动)。
怪我咯2017-04-10 14:29:43
下面这段代码可以(...js)屏蔽滚轮, 不过对于浏览器兼容性不太确定
handler = (event) -> event.preventDefault()
document.body.addEventListener 'mousewheel', handler
setTimeout ->
document.body.removeEventListener 'mousewheel', handler
, 4000
巴扎黑2017-04-10 14:29:43
//********************** Start with disable/enable scroll function **********************
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
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;
}
//********************** End with disable/enable scroll function **********************
Hope this helps
阿神2017-04-10 14:29:43
目前能想到的有
PHP中文网2017-04-10 14:29:43
这样?
http://jsfiddle.net/RayLiao/KrK7m/
搭个车顺便问一下,jsfiddle怎么样不显示或者调整tab的顺序啊?
伊谢尔伦2017-04-10 14:29:43
需要你对overflow这个css属性熟悉。弹窗时设置body的css样式overflow:hidden。弹窗要能滚动的话设置overflow:auto。即可。