js回网页顶部
2015-10-16 14:15
651 查看
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>回到顶部</title> <style type="text/css"> html{ _overflow:hidden; } body{ _overflow:auto; height:100%; } #div{ background:#ccc; width:400px; height:1500px; text-align:center; } #returntop{ border:1px #ccc solid; background:#F30; width:80px; height:30px; position:fixed; _position:absolute; line-height:30px; text-align:center; top:0px; left:0px; display:none; cursor:pointer; } a{ text-decoration:none; } </style> </head> <body> <div id="div">这是顶部</div> <div id="returntop">回顶部</div> <script type="text/javascript"> var getDiv=document.getElementById('returntop'); getDiv.onclick=function(){ window.scrollTo(0,0); } window.onscroll=function(){ if(document.documentElement.scrollTop){ getDiv.style.display="block"; }else if(document.body.scrollTop){ getDiv.style.display="block"; }else{ getDiv.style.display="none"; } } function getWinSize(){ var winHeight=window.innerHeight,winWidth=window.innerWidth; if(document.documentElement.clientHeight){ winHeight=document.documentElement.clientHeight; winWidth=document.documentElement.clientWidth; }else{ winHeight=document.body.clientHeight; winWidth=document.body.clientWidth; } var height=winHeight-50; var width=winWidth-100; getDiv.style.top=height+"px"; getDiv.style.left=width+"px"; } getWinSize(); window.onresize=getWinSize; </script> </body> </html> <div style="text-align:center;margin:30px 0 0 0;"><hr style="color:#999;height:1px;">如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></div>
相关文章推荐
- JS 纯JS幻灯片
- js生成随机数的方法实例
- js 的document.forms[0].submit()把请求提交到哪里去了
- 全栈JavaScript路(八)得知 CDATASection 种类 节点
- JavaScript Date 函数
- 在知乎看到一篇关于JavaScript书籍进阶的回答(作者:司徒正美)
- C#中Json字符串的各种应用类实例讲解
- JS 中面向对象的5种写法和拓展JS对象的写法
- javascript 观察者模式
- JS中检测数据类型的方式
- jsonp 输写格式
- 比较常用证件正则表达式验证大全
- JS Date.parse() 函数详解
- 5个经典的JavaScript面试基础问题
- ZeroclipboardJS+flash实现将内容复制到剪贴板实例
- JavaScript各类型的关系图解
- jsp自定义标签用法实例详解
- 超便捷好用的-圆形花瓣加载滚动图片插件spin.js
- JavaScript div层拖拽左右移动
- JavaScript Cookies