【问题标题】:Position absolute div in a overflow-y: scroll div将绝对 div 定位在溢出-y 中:滚动 div
【发布时间】:2015-09-03 14:59:33
【问题描述】:

我有以下 HTML:

<div class="container">
<div class="scrollable-block">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    <div class="absolute-div"></div>
</div>

还有 CSS:

.container {
    width: 200px;
    height: 300px;
    background: green;
}

.scrollable-block {
    width: 200px;
    max-height: 250px;
    overflow: scroll;
    position: relative;
}

.absolute-div {
    width: 20px;
    height: 20px;
    background: purple;
    position: absolute;
    top: 0;
    right: -10px;
}

这是一个现场演示:http://jsfiddle.net/BYTam/1/

绿色的 div 是容器,具有固定的宽度。黄色的 div 在里面并且有 max-height 和 overflow-y: 滚动。它的宽度与绿色的宽度相同。我试图绝对定位紫色 div,相对于黄色 div,但在绿色 div 之外 - 原因是我不希望黄色 div 有水平滚动条。这可能吗?

谢谢!

【问题讨论】:

  • 也许您可以将 position: relative 放在 .container 上,而不是放在 .scrollable-block 上?你可以吗?
  • 不,不幸的是,它需要与 .scrollable-block 相关。这是一个需要以这种方式运行的更复杂结构的缩小示例。

标签: css scroll position overflow absolute


【解决方案1】:

你不能用你当前的标记来做到这一点。 absolute-div 将始终触发水平滚动条,因为它嵌套在可滚动块内。似乎现代浏览器不允许使用overflow-x:visible 和overflow-y:scroll。

【讨论】:

  • 有没有办法让绝对 div 到容器 div 之外? Z 索引似乎不起作用。
  • @YavorPunchev - 不更改 HTML(或使用 JavaScript 更改 HTML/DOM)
【解决方案2】:

我认为绝对 div 不可能从滚动块中弹出。我遇到了同样的问题,这就是我解决它的方法:

 <div class="absolute-div"></div> // replace it, put it in the main container.

 $(".absolute-div").css({"top":play with this+"px"}); 
// on scroll of the "scrollable-block" change the ".absolute-div" "top" property using Jquery.

【讨论】:

    【解决方案3】:

    如果您不想要水平滚动条,只需将overflow:scroll 更改为

    overflow-y:scroll;
    overflow-x:hidden;
    

    【讨论】:

    • 是的,但是这样绝对(紫色)div 会保持隐藏状态。
    • 奇怪,对我来说紫色块在可滚动块的右上角可见
    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2018-02-09
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    相关资源
    最近更新 更多