妙味——鼠标滚轮

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
#div1{width: 200px;height: 200px;background: red;}
</style>
<!-- 
    普通事件:类似onclick 、 onmousedown...
    DOM事件:DOMMouseScroll

    DOM事件只能通过绑定来添加
 -->
<script>
function myAddEvent(obj,sEvent,fn)
{
    if(obj.attachEvent)
    {
        obj.attachEvent('on'+sEvent,fn);
    }
    else
    {
        obj.addEventListener(sEvent,fn,false);
    }
}

window.onload=function()
{
    var oDiv=document.getElementById('div1');

    function onMouseWheel(ev)
    {
        var oEvent=ev||event;

        var bDwon=true;    //鼠标滚轮方向是否向下

        bDwon=oEvent.wheelDelta?oEvent.wheelDelta < 0:oEvent.detail > 0;

        // alert(bDwon);
        if(bDwon)
        {
            oDiv.style.height=oDiv.offsetHeight+10+'px';
        }
        else
        {
            oDiv.style.height=oDiv.offsetHeight-10+'px';
        }

        if(oEvent.preventDefault)    //FF   阻止默认事件(通过事件绑定的)
        {
            oEvent.preventDefault();
        }

        return false;
    };

    myAddEvent(oDiv,'mousewheel',onMouseWheel);
    myAddEvent(oDiv,'DOMMouseScroll',onMouseWheel);
};
</script>
</head>
<body style="height:1000px;">
    <div id="div1"></div>
</body>
</html>

 

posted @ 2014-01-04 23:47  白小虫  阅读(131)  评论(0编辑  收藏  举报