博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
   body{margin:0; padding:0}
   #to_top{
  width:30px;
  height:40px;
  padding:20px;
  font:14px/20px arial;
  text - align: center;
  background: #06c;
  position: absolute;
  cursor: pointer;
  color: #fff;
 }
</style>
</head>

<body style="height:1000px;">
             <h1>返回顶部</h1 >
    <div id = "to_top"> 返回顶部
          </div>
</body>
</html>
<script type="text/javascript">
 window.onload = function(){
 var oTop = document.getElementById("to_top");
 var screenw = document.documentElement.clientWidth || document.body.clientWidth;
 var screenh = document.documentElement.clientHeight || document.body.clientHeight;
 oTop.style.left = screenw - oTop.offsetWidth +"px";
 oTop.style.top = screenh - oTop.offsetHeight + "px";
 window.onscroll = function(){
   var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
   oTop.style.top = screenh - oTop.offsetHeight + scrolltop +"px";
  } 
 oTop.onclick = function(){
  document.documentElement.scrollTop = document.body.scrollTop =0;
  }

/**jquery的写法实现返回顶部
 jQuery("#to_top").click(function(){
  jQuery("html,body").animate({'scrollTop':0},100)
 })
**/

</script>