簡體   English   中英

使用javascript將新內容添加到div時自動將div滾動到底部

[英]autoscroll a div to the bottom on adding new content to the div using javascript

我有一個 div,它從文本區域獲取新內容,如圖所示

<div style="overflow: auto; overflow-x: hidden; height:296px;" id="container"></div>

當 div 填充到最大高度時,我試圖通過自動向下滾動來實現 div 應顯示新內容,這是我的嘗試

for (var i = 0; i < json.length; i++) {
                        ....
                    document.getElementById('container').innerHTML = contents;
                    //this is the code I am trying to use to make it scroll automatically down when new content is added
                    document.getElementById('container').scrollBottom = y;

                } else {
                    alert('Request failed.  Returned status of ' + xhr.status);

當 div 達到 296px 時,div 中的內容不會自動向下滾動。 請問可能有什么問題?

我能夠用這個解決我的問題

$("#container").scrollTop($("#container")[0].scrollHeight);

以上取代了這個:

document.getElementById('container').scrollBottom = y;

我的問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM