转载自寂寞的天下着忧郁的雨 博客 html 代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>js滚动条回到底部</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> <style type="text/css"> body{ padding:0; margin:0; } .box{ position:relative; height:500px; width:100%; overflow-y:scroll; } .box1{ height:1000px; width:100%; background:red; } .box2{ height:1000px; width:100%; background:green; } .box3{ height:1000px; width:100%; background:gray; } .box4{ height:1000px; width:100%; background:yellow; } .box5{ height:1000px; width:100%; background:blue; } .box6{ height:2000px; width:100%; background:#eaeaea; } .scroll{ height:30px; width:30px; background:#4CC9C8; position:fixed; right:40px; top:450px; display:block; } </style> </head> <body> <div class="box" id=’box’> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> <div class="scroll" id=’scroll’></div> </div> <script type="text/javascript"> window.onload=function(){ var oScroll=document.getElementById(‘scroll’), box=document.getElementById(‘box’), time=null, spped=0, boxH=box.scrollHeight, scrollT=box.scrollTop; console.log(boxH,scrollT) function scroll(boxH,scrollT){ speed=20; clearInterval(time) time=setInterval(function(){ if(scrollT<boxH){ scrollT+=speed; box.scrollTop=scrollT; }else{ clearInterval(time) } },10) } scroll(boxH,scrollT); oScroll.onclick=function(){ boxH=box.scrollHeight; scrollT=box.scrollTop; scroll(boxH,scrollT); } } </script> </body> </html>
评论前必须登录!
注册