【问题标题】:How to make middle nested div scroll and top and bottom nested div maintain position fixed?如何使中间嵌套 div 滚动和顶部和底部嵌套 div 保持位置固定?
【发布时间】:2021-07-23 16:41:19
【问题描述】:

我有一个包含 3 个内部 div 的父容器。我只希望中间的内部 div 水平滚动。这是我的代码:

<div class="outer-container">
    <div class="top-inner-container">
        xoxoxoxoxoxo    
    </div>
    <div class="middle-inner-container">
        only this must scroll ===> yeah yeah yeah        
    </div>
    <div class="bottom-inner-container">
        oxoxoxoxoxox
    </div>
</div>
.outer-container {
    height: 470px;
    position: relative;
}
.top-inner-container {
    position: absolute;
    width: 100%;
    top: 0px;
    left: 0px;
    height: 70px;
}
.middle-inner-container {
    white-space: nowrap;
    width: 1480px;
    background-color: grey;
    overflow-x: scroll;
    position: absolute;
    top: 60px;
    left: 0px;
    height: 340px; 
    display: flex; 
}
.bottom-inner-container {
    position: absolute;
    // background-color: #fff;
    width: 100%;
    top: 404px;
    left: 0px;
}

我尝试了很多变化,这是我得到的最好的,但这里所有 3 个 div 滚动。 知道如何解决这个问题吗?

更新: 重申。而不是让中间 div 水平滚动所有 3 个 div 滚动。滚动条出现在父级上,而不是仅出现在具有中间内部容器类的 div 上。期望的结果是只有具有中间内部容器类的 div 水平滚动。

重要
现在忽略垂直滚动。我只是意识到垂直滚动的工作内容更多。我不关心垂直滚动只是水平滚动。顶部和底部不应水平滚动,只有中间 div 应水平滚动。水平滚动时,“xoxoxoxoxo”应始终在屏幕上可见。谢谢。

这是fiddle

【问题讨论】:

    标签: html css angular


    【解决方案1】:

    您可以通过将white-space: nowrap; 添加到.middle-inner-container 类来使中间容器水平滚动工作。

    css 看起来像这样:

    .outer-container {
        height: 470px;
        position: relative;
        width: 100%;
        .top-inner-container {
            width: 100%;
            height: 70px;
        }
        .middle-inner-container {
            white-space: nowrap;
            background-color: grey;
            overflow-x: scroll;
            height: 340px;
            .block {
                display: inline-block;
                vertical-align: top;
            }
        }
        .bottom-inner-container {
            // background-color: #fff;
            width: 100%;
            top: 404px;
            left: 0px;
        }
    }
    

    记得在中间 div 的每个小节中添加 class 块。

    【讨论】:

    • 空白:nowrap 无效
    • 据我了解,您希望通过添加更多文本来使中间 div 水平滚动工作,它只是被容器包裹,而 x-scroll 不起作用。能否更感谢您澄清问题
    • 我在更新中澄清了。看看我更新的问题。谢谢。
    • 我要在中间部分添加会占用大量横向空间的内容。这个中间容器应该是唯一的 scrobble(水平)容器,并且顶部和底部应该始终在屏幕上可见。这就是我尝试固定位置但顶部、中间和底部滚动的方法。
    • 我添加了巨大的文本内容,所有三个 div 仍然可见,但中间容器垂直滚动工作。你想让它在你设置的中间容器高度内水平滚动而不是垂直滚动,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2012-03-13
    • 1970-01-01
    • 2023-01-24
    相关资源
    最近更新 更多