保持溢出div滚动到底部,除非用户向上滚动 [英] Keep overflow div scrolled to bottom unless user scrolls up

查看:82
本文介绍了保持溢出div滚动到底部,除非用户向上滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,只有300像素大,我想要的,当页面加载滚动到内容的底部。这个div具有动态添加的内容,需要保持滚动一直向下。现在,如果用户决定向上滚动,我不想让它跳回到底部,直到用户再次向下滚动。

I have a div that is only 300 pixels big and I want it to when the page loads scroll to the bottom of the content. This div has content dynamically added to it and needs to stay scrolled all the way down. Now if the user decides to scroll up I don't want it to jump back to the bottom until the user scrolls all the way down again

可以有一个div将保持滚动到底部,除非用户向上滚动,并且当用户滚动回到底部时,即使添加新的动态内容也需要将其保持在底部。

Is it possible to have a div that will stay scrolled to the bottom unless the user scrolls up and when the user scrolls back to the bottom it needs to keep itself at the bottom even when new dynamic content is added. How would I go bout creating this.

推荐答案

这可能会帮助你:

var element = document.getElementById("yourDivID");
element.scrollTop = element.scrollHeight;

,以匹配评论...

, to match the comment...

function updateScroll(){
    var element = document.getElementById("yourDivID");
    element.scrollTop = element.scrollHeight;
}

每当添加内容时,调用 updateScroll / code>,或设置一个计时器:

whenever content is added, call updateScroll(), or set a timer:

//once a second
setInterval("updateScroll",1000);

如果您想仅在用户未移动时更新:

if you want to update ONLY if the user didn't move:

var scrolled = false;
function updateScroll(){
    if(!scrolled){
        var element = document.getElementById("yourDivID");
        element.scrollTop = element.scrollHeight;
    }
}

$("#yourDivID").on('scroll', function(){
    scrolled=true;
});

这篇关于保持溢出div滚动到底部,除非用户向上滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆