【问题标题】:Make element stay to the right even when scrolling horizontally即使水平滚动也使元素保持在右侧
【发布时间】:2018-01-22 00:29:19
【问题描述】:

有一个“红色”元素应始终保持在右侧。向右是指父元素lime colored border 的右侧。 “通常”将父级设置为position: relative;,将子级设置为position: absolute; right: 0; 可以解决问题,但在这种情况下不会。

谁能告诉我为什么以及如何解决它?

.grid {
    display: flex;
    flex-wrap: nowrap;
    width: 100%;
    margin: 0;
}

.grid div[class*='col-'] {
    position: relative;
    padding: 15px;
    flex-grow: 0.33;
    flex-shrink: 1;
    flex-basis: 0%;
    border: solid 1px lime;
}

.resize-bar {
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
    width: 5px;
    background-color: red;
    opacity: 0.8;
}

[data-resizable] {
    min-width: 5px;
    overflow-x: scroll;
}
<h2>Hover on "orange" and scroll horizontally</h2>
<div class="grid">
  <div class="col-1-4" data-resizable="true">
      <div style="width:300px;height:40px;background-color:orange;"></div>
      <span class="resize-bar"></span>
  </div>
</div>

如果可能,将首选非 javascript 解决方案。谢谢。

JsFiddle here

【问题讨论】:

  • 你不能将调整大小栏放在父元素之外吗?这应该可以解决您的问题。
  • @Mr_Green-divami.com 不是真的。然后我不知道在哪里放置resize-bar,因为col-1-4 可以有不同的宽度,并且可以有多个col-1-4 元素。
  • border: solid 1px lime; 之后添加border-right: solid 5px red;。从您的标记中删除 &lt;span class="resize-bar"&gt;&lt;/span&gt;
  • 需要弹性吗?
  • 这太愚蠢了。你需要改变结构,你真的给已经设计好的元素加上 1 个小节来移动。我们甚至不知道您是否可以设置 px 大小。我根本不建议在这里使用 flex,如果你想要基于 vw、vh 或 em、rem 的可扩展对象,它可以非常简单地修复,但是在所有情况下,flex 在纯相对定位的情况下都会失败。

标签: javascript html css


【解决方案1】:

fiddle

将内容包装在另一个div 中,与col- 大小相同

.grid {
    display: flex;
    flex-wrap: nowrap;
    width: 100%;
    margin: 0;
}

.grid div[class*='col-'] {
    position: relative;
    flex-grow: 0.33;
    flex-shrink: 1;
    flex-basis: 0%;
    overflow-x: hidden;
    border: solid 1px lime;
}

.resize-bar {
    position: absolute;
    display: block;
    right: 0;
    top: 0;
    height: 100%;
    width: 5px;
    background-color: red;
    opacity: 0.8;
}

.box {
  width: 100%;
  height: 100%;
    min-width: 5px;
    overflow-x: scroll;
}
<h2>Hover on "orange" and scroll horizontally</h2>
<div class="grid">
  <div class="col-1-4" data-resizable="true">
    <div class="box">
      <div style="width:370px;height:40px;background-color:orange;">

      </div>
    </div>
    <span class="resize-bar"></span>
  </div>
      
 
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2013-12-15
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多