Home  >  Article  >  Web Front-end  >  How to use js to control the scroll bar of a div so that it automatically scrolls to the bottom when the content is updated

How to use js to control the scroll bar of a div so that it automatically scrolls to the bottom when the content is updated

高洛峰
高洛峰Original
2016-12-08 16:06:441322browse

As shown below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
 
<script type="text/javascript">
function add()
{
var now = new Date();
var div = document.getElementById(&#39;scrolldIV&#39;);
div.innerHTML = div.innerHTML + &#39;time_&#39; + now.getTime() + &#39;<br />&#39;;
div.scrollTop = div.scrollHeight;
}
</script>
<div id="scrolldIV" style="overflow:auto; height: 100px; width: 400px; border: 1px solid #999;">
</div>
<input type="button" value="插入一行" onclick="add();">
用js控件div的滚动条,让它在内容更新时自动滚到底部</body>
</html>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn