<!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>Javascript 返回顶部</title> <style type="text/css"> #btn {width:40px; height:40px; position:fixed; display:none;right:65px; bottom:10px; background:url(images/top_bg.png) no-repeat left top;} #btn:hover {background:url(images/top_bg.png) no-repeat 0 -39px;} .bg {width:1190px; margin:0 auto;} </style> </head> <body> <a href="javascript:;" id="btn" title="回到顶部"></a> <div> <img src="images/tb_bg.jpg" alt="" /> </div> <script> window.onload=function(){ var time=null; var btn=document.getElementById("btn") btn.onclick=function(){ time=setTimeout(app,300) function app(){ var scroll=document.body.scrollTop||document.documentElement.scrollTop; document.body.scrollTop=0; } } var clientHeight=document.documentElement.clientHeight; var scroll=document.documentElement.scrollTop||document.body.scrollTop; window.onscroll=function(){ if(scroll>=clientHeight/2){ btn.style.display="blcok"; } else{btn.style.display="none"} } } </script> </body> </html>
滚动没有触发事件,btn没有出现,不知道哪里错了
相关分类