利用jquery實現頁面可視區滾動到指定位置。直接上代碼
//滾動到指定位置 function scrollTo(element,speed) { if(!speed){ speed = 300; } if(!element){ $("html,body").animate({scrollTop:0},speed); }else{ if(element.length>0){ $("html,body").animate({scrollTop:$(element).offset().top-200},speed); } } } //然后觸發某個事件時調用這個函數。 //scrollTo('#comment',300); 寫評論功能的時候,滾動到文本域的位置。#comment是文本域ID屬性。
offset() 返回或設置匹配元素相對於文檔的偏移。上述代碼中 -200 的意思是是文本域再向下移動200px,實現此功能主要是scrollTop配合animate動畫。
//回答頂部 $('html,body').animate({scrollTop: 0},500); //距離body頂部為0,scrollTop
參考:scrollTop講解
$("html,body")是什么意思呢?暫時沒明白。