即使水平滚动,也要使元素保持在右侧

时间:2017-08-14 07:58:51

标签: javascript html css

有一种红色的"应始终保持正确的元素。在右边,我指的是父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>
&#13;
&#13;
&#13;

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

JsFiddle here

1 个答案:

答案 0 :(得分:2)

fiddle

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

相同

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;