Auto-scroll to the bottom of a div

后端 未结 3 636
长发绾君心
长发绾君心 2021-01-25 18:05

I have a div with overflow set to scroll which essentially streams data line by line off a file. I\'d like to scroll automatically to the bottom of the div whenever the stream o

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 18:45

    Some old code of mine with a running example that will stay at the bottom when new content is added, if the user scrolls it will not more it to the bottom.

    var chatscroll = new Object();
    chatscroll.Pane = 
        function(scrollContainerId)
        {
            this.bottomThreshold = 25;
            this.scrollContainerId = scrollContainerId;
        }
    
    chatscroll.Pane.prototype.activeScroll = 
        function()
        {
            var scrollDiv = document.getElementById(this.scrollContainerId);
            var currentHeight = 0;
    
            if (scrollDiv.scrollHeight > 0)
                currentHeight = scrollDiv.scrollHeight;
            else 
                if (objDiv.offsetHeight > 0)
                    currentHeight = scrollDiv.offsetHeight;
    
            if (currentHeight - scrollDiv.scrollTop - ((scrollDiv.style.pixelHeight) ? scrollDiv.style.pixelHeight : scrollDiv.offsetHeight) < this.bottomThreshold)
                scrollDiv.scrollTop = currentHeight;
    
            scrollDiv = null;
        }
    

提交回复
热议问题