鼠标滚轮滚动事件

时间:2012-12-27 08:55:43

标签: javascript jquery mouseevent mousewheel

我想要的是:当鼠标指向div时,页面的滚动条不会滚动。这不可能吗?当我这样做时,页面的滚动条总是滚动。这是一段javascript代码:

if(document.addEventListener){
    document.addEventListener('DOMMouseScroll',scrollFunc,false);
}
window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome/Safari

怎么做?

2 个答案:

答案 0 :(得分:4)

你走了:

var noscroll = document.getElementById('noscroll');

var locked, lockedX, lockedY;
noscroll.addEventListener('mouseover', function (){
    locked = true;
    lockedX = window.scrollX;
    lockedY = window.scrollY;
}, false);
noscroll.addEventListener('mouseout', function (){
    locked = false;
}, false);

window.addEventListener('scroll', function (e){
    if(locked === true){
        window.scrollTo(lockedX, lockedY);
        e.preventDefault();
    }
}, false);

将变量noscroll更改为您不想允许滚动的元素。

Demo

答案 1 :(得分:0)

您基本上可以通过为div指定widthheightoverflow属性来实现它:

<div style="width:100px; height:100px; overflow: auto;" >
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
    text text text text text text text text text
</div>