Javascript實現(xiàn)DIV滾動自動滾動到底部的代碼
更新時間:2012年03月01日 22:47:57 作者:
一個比較特殊的客戶要求,在一個頁面用表格顯示數(shù)據(jù),數(shù)據(jù)量不是很多,不希望使用瀏覽器的滾動條,只能在Div中滾動table中的數(shù)據(jù),但是有個特殊的要求,就是必須將滾動條自動滾動到底部
查詢了一下相關(guān)的資料,Div沒有自動滾動的屬性,只能模擬鼠標的滾動來現(xiàn)實想要的效果。
關(guān)鍵的部分部分在這里:div.scrollTop = div.scrollHeight;
下面是具體實現(xiàn)的精簡代碼:
<html>
<body>
<div id="divDetail" style="overFlow-y:scroll; width:250px;height: 200px;">
<table style="border:1px solid; ">
<tr><td>id</td><td>name</td><td>age</td><td>memo</td></tr>
<tr><td>000001</td><td>name1</td><td>24</td><td>memomemomemomemomemo</td></tr>
<tr><td>000002</td><td>name2</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000003</td><td>name3</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000004</td><td>name4</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000005</td><td>name5</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000002</td><td>name2</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000003</td><td>name3</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000004</td><td>name4</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000005</td><td>name5</td><td>23</td><td>memomemomemomemomemo</td></tr>
</table>
</div>
</body>
<script type="text/javascript" defer>
var div = document.getElementById('divDetail');
div.scrollTop = div.scrollHeight;
//alert(div.scrollTop);
</script>
</html>
其實,實現(xiàn)是很簡單的但是一般很少有這種需求,期間還是走了一些彎路。
關(guān)鍵的部分部分在這里:div.scrollTop = div.scrollHeight;
下面是具體實現(xiàn)的精簡代碼:
復(fù)制代碼 代碼如下:
<html>
<body>
<div id="divDetail" style="overFlow-y:scroll; width:250px;height: 200px;">
<table style="border:1px solid; ">
<tr><td>id</td><td>name</td><td>age</td><td>memo</td></tr>
<tr><td>000001</td><td>name1</td><td>24</td><td>memomemomemomemomemo</td></tr>
<tr><td>000002</td><td>name2</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000003</td><td>name3</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000004</td><td>name4</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000005</td><td>name5</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000002</td><td>name2</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000003</td><td>name3</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000004</td><td>name4</td><td>23</td><td>memomemomemomemomemo</td></tr>
<tr><td>000005</td><td>name5</td><td>23</td><td>memomemomemomemomemo</td></tr>
</table>
</div>
</body>
<script type="text/javascript" defer>
var div = document.getElementById('divDetail');
div.scrollTop = div.scrollHeight;
//alert(div.scrollTop);
</script>
</html>
其實,實現(xiàn)是很簡單的但是一般很少有這種需求,期間還是走了一些彎路。
相關(guān)文章
IScroll那些事_當內(nèi)容不足時下拉刷新的解決方法
下面小編就為大家?guī)硪黄狪Scroll那些事_當內(nèi)容不足時下拉刷新的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07JavaScript JSON數(shù)據(jù)處理全集(小結(jié))
這篇文章主要介紹了JavaScript JSON數(shù)據(jù)處理全集,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08一個不錯的應(yīng)用,用于提交獲取文章內(nèi)容,不推薦用
一個不錯的應(yīng)用,用于提交獲取文章內(nèi)容,不推薦用...2007-03-03