【问题标题】:Inner div element scroll via outer scrollbar内部 div 元素通过外部滚动条滚动
【发布时间】:2014-05-15 21:37:39
【问题描述】:

我有一个垂直滚动的 div 元素;滚动条位于 div 元素本身内。我想在 div 之外有滚动条,类似于典型的网页滚动条。滚动条看起来像任何其他网页,但滚动时只会滚动这个特定的 div 元素。

<div id="outer">
    <div id="inner"> //to be scrolled
        <!-- content -->
    </div>
</div>

小提琴示例:http://jsfiddle.net/7pgyt/

我希望滚动条位于最右侧的红色区域。滚动条滚动蓝色区域。这可以仅在 HTML 和 CSS 中完成吗?

可能的结果如下:

【问题讨论】:

标签: html css scroll


【解决方案1】:

给定以下 html 结构:

<div id="wrapper">
    <div id="top" class="bar"></div>
    <div id="outer">
        <div id="inner"></div>
    </div>
    <div id="bottom" class="bar"></div>
</div>

您可以使用以下样式:

#wrapper {
    position:relative;
    height:200px;
}

#outer {
    background-color: red;
    overflow-y:auto;
    height: 200px;
}

#inner {
    color: white;
    font-weight: bold;
    margin-left: auto;
    margin-right: auto;
    background-color: blue;
    margin:50px;
    position:relative; z-index:1;
}

.bar {
    height:50px; 
    z-index:2;
    position:absolute; 
    background:red; 
    left:0; 
    right:20px;
}

#bottom {bottom:0;}
#top {top:0; }

Example

Example with psuedo selectors instead of the bar divs

【讨论】:

  • 更喜欢这个答案(超过 SW4),因为它不涵盖内容(这会阻止使用鼠标滚轮进行选择和滚动)。 +1
【解决方案2】:

您可以使用以下内容:

Demo Fiddle

HTML

<body>
    <div id="outer"></div>
    <div id="inner">..content...</div>
</body>

CSS

body {
    position:relative;
    width:100%;
    margin:0;
    padding:0;
}
div {
    box-sizing:border-box;
}
#outer {
    border:50px solid red;
    padding: 50px;
    position:absolute;
    height:200px;
    right:17px;
    left:0;
}
#inner {
    color: white;
    font-weight: bold;
    height: 200px;
    padding:50px;
    margin-left: auto;
    margin-right: auto;
    overflow-y: scroll;
    background-color: blue;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多