js实现将div等滚动条(scrollbar)保持在最底部的方法,很多时候我们要让程序的滚动条保持在底部,如聊天室,下面将利用div+css+javascript实现保持滚动条在最底部。
代码如下,
<style type="text/css"> .cdiv{ background-color: white; height: 100px; width: 380px; padding-left: 0; overflow-y: scroll; position: relative; word-wrap: break-word; overflow-x: auto; text-align:left; } </style> <div id="divmsg" class="cdiv"></div> <input id="test" type="button" value="测试" /> <script type="text/javascript"> var test = document.getElementById("test"); var div = document.getElementById("divmsg"); test.onclick = function () { div.innerHTML = div.innerHTML+"js实现将div等滚动条(scrollbar)保持在最底部<br/><br/>"; boxScroll(div); } function boxScroll(o){ o.scrollTop = o.scrollHeight; } </script>