简体   繁体   English

Div Overflow Scroll-bottom:有可能吗?

[英]Div Overflow Scroll-to-bottom: Is it possible?

If I have a div with overflow:auto so that it is a scrollable div and I load it with information that makes a significant scroll area, is there a way that when I load the information, the div shows the bottom results? 如果我有一个带overflow:autodiv overflow:auto那么它是一个可滚动的div ,我加载了一个显着滚动区域的信息,是否有一种方法,当我加载信息时, div显示底部结果? Or essentially scrolls to the bottom? 或者基本上滚动到底部?

I've seen jQuery solutions but this is for use in an HTA so I cannot use jQuery. 我见过jQuery解决方案,但这是用于HTA所以我不能使用jQuery。 Is there a purely javascript way to accomplish this? 有没有纯粹的JavaScript方式来实现这一目标?

var myDiv = document.getElementById('myDiv');
myDiv.scrollTop = myDiv.scrollHeight;

Works in Firefox, Safari, Opera, Chrome and even Internet Explorer, which is more than I can say for the SSE test case I Set up... lol 可以在Firefox,Safari,Opera,Chrome甚至Internet Explorer中使用,这比我设置的SSE测试案例更能说明... lol

I will spare you the rant about the obtuse solutions offered by others, and here is an example of code that could be used for an instant messaging type client. 我将免除你对其他人提供的迟钝解决方案的咆哮,这里是一个可用于即时消息类型客户端的代码示例。

document.body.onload = function()
{
    var myDiv = document.getElementById('myDiv');
    // Pick your poison below, server sent events, websockets, AJAX, etc.
    var messageSource = new EventSource('somepage');
    messageSource.onmessage = function(event)
    {
        // You must add border widths, padding and margins to the right.
        var isScrolled = myDiv.scrollTop == myDiv.scrollHeight - myDiv.offsetHeight;
        myDiv.innerHTML += event.data;
        if(isScrolled)
            myDiv.scrollTop = myDiv.scrollHeight;
    };
};

The part of that example that is relevant checks to see if the div is already scrolled to the bottom, and if it is, scrolls it to the bottom after adding data to it. 该示例中相关的部分检查div是否已经滚动到底部,如果是,则在向其添加数据后将其滚动到底部。 If it is not already scrolled to the bottom, the div's scroll position will stay such that the visible content of the div is unaffected by adding the data. 如果它尚未滚动到底部,则div的滚动位置将保持不变,以便div的可见内容不受添加数据的影响。

document.getElementById('mydiv').scrollTop = 9999999;

The scrollTop property specifies the scrolling offset in pixels from the top of the region. scrollTop属性指定从区域顶部开始的滚动偏移(以像素为单位)。 Setting it to a very large value will force it to the bottom. 将其设置为非常大的值将强制它到底部。

How about this? 这个怎么样?

function scroll_to_max(elm) { // {{{
    if(!scroll_to_max_el) {
        scroll_to_max_el = elm;
        setTimeout(scroll_to_max, 10); // Allow for the element to be updated
    } else {
        var el = scroll_to_max_el;
        var t = el.scrollTop;
        el.scrollTop = t+100;
        if(el.scrollTop != t) {
            setTimeout(scroll_to_max, 10); // Keep scrolling till we hit max value
        } else {
            scroll_to_max_el = null;
        }
    }
}
var scroll_to_max_el = null; // Global var!
// }}}

(NOTE: Only tested it in Chrome...) (注意:仅在Chrome中测试过...)

迟到的答案,但这更有帮助

$('#mydiv').scrollTop(($('#mydiv').height()*2));

I think you need to set the scrollTop after the element is updated. 我认为你需要在元素更新设置scrollTop。

setTimeout(function (){ 
  el.scrollTop = 999999999
}, 10)

also, in chrome at least, 99999999999 will scroll to the bottom. 此外,至少在chrome中,99999999999将滚动到底部。 but 999999999999 (an extra 9) will scroll to the top. 但是999999999999(额外的9)将滚动到顶部。 it's probably converted to an int in the C side of webkit. 它可能转换为webkit的C端的int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM