js出现滚动条后出现返回顶部按钮,支持所有浏览器

<!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" />
<style>
.backToTop{display:none; width:18px; line-height:1.2; padding:5px 0;  color:#fff; font-size:12px; text-align:center; position:fixed; _position:absolute; right:10px; bottom:100px; _bottom:"auto"; cursor:pointer; opacity:.6; filter:Alpha(opacity=60);}
</style>
</head>

<body>
<br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
            <br />
            <br /><br />
            <br />
            <br />
            <br /><br />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
(function() {
 var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
  .text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
   $("html, body").animate({ scrollTop: 0 }, 120);
 }), $backToTopFun = function() {
  var st = $(document).scrollTop(), winh = $(window).height();
  (st > 0)? $backToTopEle.show(): $backToTopEle.hide(); 
  //IE6下的定位
  if (!window.XMLHttpRequest) {
   $backToTopEle.css("top", st + winh - 166); 
  }
 };
 $(window).bind("scroll", $backToTopFun);
 $(function() { $backToTopFun(); });
})();
</script>
</body>
</html>

posted @ 2015-10-09 20:56  z542601362  阅读(310)  评论(0)    收藏  举报