首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。