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