三種控制DIV內容滾動的方法:
本人qq群也有許多的技術文檔,希望可以為你提供一些幫助(非技術的勿加)。
QQ群: 281442983 (點擊鏈接加入群:http://jq.qq.com/?_wv=1027&k=29LoD19)
方法一:
使用錨標記要滾動到的位置,然后通過click方法模擬點擊滾動到錨所在位置
<script language="javascript1.2" type="text/javascript">
function onGetMessage(context)
{
msg.innerHTML+=context;
msg_end.click();
}
</script>
<div style="width:500px;overflow:auto">
<div id="msg" style="overflow:hidden;width:480px;"></div>
<div><a id="msg_end" name="1" href="#1"> </a></div>
</div>
方法二:
利用DIV的scrollIntoView方法,將最底端滾動到可視位置 [list=1]<script
language="javascript1.2"
type="text/javascript">
function onGetMessage(context)
{
msg.innerHTML+=context;
msg_end.scrollIntoView();
}
</script>
<div style="width:500px;overflow:auto">
<div id="msg" style="overflow:hidden;width:480px;"></div>
<div id="msg_end" style="height:0px; overflow:hidden"></div>
</div>
方法三:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="keywords" content="滾動條, scrollbar, 頁面底部, 聊天窗口, " />
<meta name="description" content="有些時候(如開發聊天程序),我們需要將將滾動條(scrollbar)保持在最底部,比如聊天窗口,最新發出和收到的信息要顯示在最下方,如果要看到最下方的內容,就必須保證滾動條保持在最底部。" />
<title>將滾動條(scrollbar)保持在最底部的方法 - 滾動條, scrollbar, 頁面底部, 聊天窗口, </title>
</head>
<body>
<div id="example">
<h3 id="example_title">將滾動條(scrollbar)保持在最底部的方法</h3>
<div id="example_main">
<!--************************************* 實例代碼開始 *************************************-->
<script type="text/javascript">
function add()
{
var now = new Date();
var div = document.getElementById('scrolldIV');
div.innerHTML = div.innerHTML + 'time_' + now.getTime() + '<br />';
div.scrollTop = div.scrollHeight;
}
</script>
<span class="notice">請點擊“插入一行”按鈕,插入最新信息,當出現滾動條時,滾動條將自動保持在底部。</span><br />
<div id="scrolldIV" style="overflow:auto; height: 100px; width: 400px; border: 1px solid #999;">
</div>
<input type="button" value="插入一行" onclick="add();">
<!--************************************* 實例代碼結束 *************************************-->
</div>
</div>
</body>
</html>
本人qq群也有許多的技術文檔,希望可以為你提供一些幫助(非技術的勿加)。
QQ群: 281442983 (點擊鏈接加入群:http://jq.qq.com/?_wv=1027&k=29LoD19)
我的淘寶店,可以進去逛逛噢:https://shop108912636.taobao.com/index.htm?spm=2013.1.w5001-7867000954.3.1d29318dPlLar7&scene=taobao_shop