首页 > 代码库 > addEventListener || attachEvent

addEventListener || attachEvent

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>addevent</title></head><body style="height:9000px;"><div style="position:fixed; top:0px; left:0px;"><div id="test"></div><div id="test2"></div></div><script>    var $ = function(id){        return document.getElementById(id);    }    var addEvent = function(obj,event,fn){        if(obj.addEventListener){            obj.addEventListener(event,fn,false);        }else if(obj.attachEvent){            obj.attachEvent("on"+event,fn);        }    }    var scrollEvent = function(){        var screenHeight =document.documentElement.clientHeight||document.body.clientHeight;        var scrollHeight = document.documentElement.scrollTop||document.body.scrollTop;        $(test).innerHTML= "可视高度:" + screenHeight;        $("test2").innerHTML= "滚动高度:" + scrollHeight;    }    addEvent(window,load,function(){        scrollEvent();    });    addEvent(window,scroll,function(){        scrollEvent();    });        addEvent(window,resize,function(){        scrollEvent();    });</script>    </body></html>

 

addEventListener || attachEvent