天天看點

JavaScript實作頁面傳回頂部

JS代碼:

<script type="text/javascript">
  	//實作回到頁面頂部
	function goTopEx(){
        var obj=document.getElementById("goTopBtn");
        function getScrollTop(){
        		//谷歌浏覽器
                return document.body.scrollTop;
                //IE浏覽器
                //return document.documentElement.scrollTop;
            }
        function setScrollTop(value){
        		//谷歌浏覽器
                document.body.scrollTop=value;
                //IE浏覽器
                //document.documentElement.scrollTop=value;
            }    
        window.οnscrοll=function(){getScrollTop()>100?obj.style.display="":obj.style.display="none";}
        obj.οnclick=function(){
            var goTop=setInterval(scrollMove,10);
            function scrollMove(){
                    setScrollTop(getScrollTop()/1.1);
                    if(getScrollTop()<1)clearInterval(goTop);
                }
        }
    }
  </script>
           

CSS代碼:

<style type="text/css">
#goTopBtn {
	position: fixed; text-align: center; line-height: 30px; width: 30px; bottom: 150px; height: 33px; font-size: 12px; cursor: pointer; right: 100px; _position: absolute; _right: auto
}
</style>
           

HTML代碼:

<DIV style="display: none" id=goTopBtn><IMG image/top1.gif"></DIV>
<script type=text/javascript>goTopEx();</script> 
           

繼續閱讀