js實現點擊按鈕滾動條緩慢滾動到頂部


            toTop:function(){
                //toTop 滾動到頂部
                var currentPosition,timer;
                var speed=10;
                timer=setInterval(function(){
                    currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
                    currentPosition-=speed; //speed變量
                    if(currentPosition>0){
                        window.scrollTo(0,currentPosition);
                    }else{
                        window.scrollTo(0,0);
                        clearInterval(timer);
                    }
                },1);
            }    

 

【注意】document.body.scrollTop的值一直是0的情況:

1.頁面指定了DTD,即指定了DOCTYPE時,使用document.documentElement

2.頁面沒有DTD,即沒指定DOCTYPE時,使用document.body


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM