滚动具有绝对位置的内容

发布于 2025-01-07 17:23:34 字数 1485 浏览 0 评论 0原文

我有一个具有固定高度的 div(.outer - 视口),其中包含另一个应该可滚动的 div(.inner - 内容)。 .inner div 使用 bottom: 0 进行绝对定位,以便我们始终可以看到该 div 底部的内容。

问题是,当这个 bottom: 0 添加到 .inner 中时,.outer 的滚动条变成灰色并且不活动。如果我们删除 bottom: 0,滚动条可以工作,但我们会看到 .inner 的顶部,而不是底部。

这是 HTML:

<div class="outer">
    <div class="inner">
        this content should be scolled to the bottom possibly with bottom: 0; AND we should still have to scrollbar the scroll to the top<br/>
        1<br/>
        2<br/>
        3<br/>
        4<br/>
        5<br/>
        6<br/>
        7<br/>
        8<br/>
        9<br/>
        10<br/>
        11<br/>
        12<br/>
        13<br/>
        14<br/>
        15<br/>
        16<br/>
        17<br/>
        18<br/>
        19<br/>
        20<br/>
        bottom (the scrollbar must be active)
    </div>
</div>​

CSS:

.outer {
    height: 200px; 
    position: relative;
    overflow-y: scroll;
    width:270px;
}
.inner {
    position: absolute;
    bottom: 0px;
    background-color:#aaa;
    width:250px;
}

http://jsfiddle.net/SAyhn/8/

使用 jQuery在 .outer 的视口中显示 .inner 的底部也是可以接受的。

I have a div (.outer - the viewport) with fixed height that contains another div (.inner - the content) which should be scrollable. The .inner div is positioned absolute with bottom: 0 so that we can always see what's on the bottom of this div.

The problem is that when this bottom: 0 is added to .inner, the scrollbar of the .outer becames gray and is not active. If we remove the bottom: 0, the scrollbar works, but we see the top, and not the bottom of the .inner.

This is the HTML:

<div class="outer">
    <div class="inner">
        this content should be scolled to the bottom possibly with bottom: 0; AND we should still have to scrollbar the scroll to the top<br/>
        1<br/>
        2<br/>
        3<br/>
        4<br/>
        5<br/>
        6<br/>
        7<br/>
        8<br/>
        9<br/>
        10<br/>
        11<br/>
        12<br/>
        13<br/>
        14<br/>
        15<br/>
        16<br/>
        17<br/>
        18<br/>
        19<br/>
        20<br/>
        bottom (the scrollbar must be active)
    </div>
</div>​

CSS:

.outer {
    height: 200px; 
    position: relative;
    overflow-y: scroll;
    width:270px;
}
.inner {
    position: absolute;
    bottom: 0px;
    background-color:#aaa;
    width:250px;
}

http://jsfiddle.net/SAyhn/8/

Using jQuery to show the bottom of the .inner in the .outer's viewport is also acceptable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2025-01-14 17:23:34

在您的示例中,外部 div 实际上并未向下滚动。发生的情况是内部 div 相对于外部 div 定位。由于它是 bottom:0,这意味着它填充了整个 div,其余部分溢出到元素上方,因此没有任何内容可以滚动。 (您可以尝试将其设置为 bottom:-10px ,为滚动提供 10 像素的空间。)您

无法单独使用 CSS 设置滚动位置,您需要一些 JavaScript:

var outer = document.getElementsByClassName( 'outer' );
outer[0].scrollTo( 0, 10000 );​​​  

或者使用 jQuery:

$( '.outer' ).scrollTop( 10000 );

The outer div isn't actually scrolled down in your example. What happens there is that the inner div is positioned in relation to the outer div. Since it's bottom:0, it means that it fills the entire div and the rest overflows above the element so there's nothing to scroll. (You can try setting it to bottom:-10px which gives 10 pixels room for a scroll.)

You can't set the scroll position with CSS alone, you need some JavaScript:

var outer = document.getElementsByClassName( 'outer' );
outer[0].scrollTo( 0, 10000 );​​​  

Or with jQuery:

$( '.outer' ).scrollTop( 10000 );
昨迟人 2025-01-14 17:23:34

实际上,我对你的建议做了进一步的了解,我将 position:absolutebottom: 0 留给了 .inner div(以获得此内容在底部(加载页面时),以防 .inner 的高度低于 .outer),我添加了以下内容:

if($('.outer').outerHeight() < $('.inner').outerHeight()) {
    $('.inner').css("bottom", "auto");
    $('.outer').scrollTop($('.inner').outerHeight());
}

这样我就有了 .inner 显示在底部(如果它的高度小于默认情况下 .outer ),每次将其他内容添加到 .inner div 中时,我都会执行上面的代码,以便在必要时进行实际滚动。

再次非常感谢。 :)

I actually went a little further with you suggestion and I left position: absolute and bottom: 0 to the .inner div (to have this content at the bottom (when the page is loaded) in case the .inner has less height that the .outer) and I added this:

if($('.outer').outerHeight() < $('.inner').outerHeight()) {
    $('.inner').css("bottom", "auto");
    $('.outer').scrollTop($('.inner').outerHeight());
}

This way I have the .inner shown at the bottom (if it has less height that the .outer) by default and I execute the code above everytime I put additional content to the .inner div to make an actual scroll if it is necessary.

Many thanks once again. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文