使用justify-content:flex-end并使用垂直滚动条

Srd*_*vic 20 css flexbox

我聊天了,我需要将所有内容滚动到底部.我想使用justify-content:flex-end和垂直滚动条.

.session-textchat {
  height: 320px;
  background: #fff;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: flex-end;
  align-items: flex-end;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  -webkit-flex-direction: column;
  flex-direction: column;
}
.session-textchat .past-messages {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  height: 83.92%;
  overflow-y: auto;
  padding: 30px 0 0;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: flex-end;
  align-items: flex-end;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
  -webkit-flex-direction: column;
  flex-direction: column;
}
.session-textchat .past-messages .receiver,
.session-textchat .past-messages .sender {
  width: 100%;
  min-height: 47px;
  margin: 0 0 20px;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: row;
  flex-direction: row;
}
.session-textchat .past-messages .receiver .message,
.session-textchat .past-messages .sender .message {
  position: relative;
  padding: 17px;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
}
.session-textchat .past-messages .receiver {
  text-align: left;
  -webkit-justify-content: flex-start;
  justify-content: flex-start;
}
.session-textchat .past-messages .receiver .message {
  background: #f4f4f4;
  color: #535353;
}
.session-textchat .past-messages .sender {
  text-align: right;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
}
.session-textchat .past-messages .sender .message {
  background: url('../img/rgbapng/0050ff26.png');
  background: rgba(0, 80, 255, 0.15);
  color: #0050ff;
}
Run Code Online (Sandbox Code Playgroud)
<div class="session-textchat">
  <div class="past-messages">
    <div class="receiver">
      <span class="message">
            Good afternoon David. Welcome to your appointment! How are you today?
          </span>
    </div>
    <div class="sender">
      <span class="message">
            Hello doctor. I feel terrible to be honest.
          </span>
    </div>
    <div class="receiver">
      <span class="message">
            I can see from your notes that you've been having some ear ache - can you tell me a bit more about your symptoms?
          </span>
    </div>
    <div class="sender">
      <span class="message">
            Hello doctor. I feel terrible to be honest.
          </span>
    </div>
    <div class="receiver">
      <span class="message">
            I can see from your notes that you've been having some ear ache - can you tell me a bit more about your symptoms?
          </span>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

示例在这里.

可能吗?或者请给我更好的解决方案.

提前致谢!
斯尔詹

Pet*_*sen 31

我只是自己不得不面对这个问题,在得出结论一个错误后,我想出了一个解决方法.

总之,不要使用justify-content: flex-end而是放在margin-top: auto第一个孩子身上.与flex-end不同,它不会破坏滚动条功能,并且当它们没有溢出容器时它会使内容对齐.

基于@SrdjanDejanovic小提琴的例子在https://jsfiddle.net/peter9477/4t5r0t5b/

如果示例不可用,这里是相关的CSS:

#container {
    overflow-y: auto;
    display: flex;
    flex-flow: column nowrap;
    /* justify-content: flex-end; DO NOT USE: breaks scrolling */
}
#container > :first-child {
    margin-top: auto !important;
    /* use !important to prevent breakage from child margin settings */
}
Run Code Online (Sandbox Code Playgroud)

我相信我也使用的另一种解决方法是为滚动条添加一个额外的容器.使用内部容器上的flex-end并让外部容器处理滚动.我一般不喜欢需要添加虚拟元素的变通方法,所以我更喜欢上面的CSS-only解决方案.

  • @lbartolic 副本?不是真的......事实上,他们之间似乎没有什么共同之处。在发布我的之前我当然没有读过你的(这不是我滚动的方式),我的包括一些你没有的背景链接(在广泛研究后收集,我可能会补充),以及页边距顶部的部分可能对我的至关重要答案不在你的范围内,所以他们可能有一些相同的小部分,但显然不是。 (3认同)
  • `#container::before{内容:''; 显示:块;顶部边距:自动;}` 比 `!important` 更好 (3认同)
  • 有趣的是,FF 问题说它是“已解决”,但我今天刚刚在最新版本中遇到了这个问题...... (2认同)
  • 这个解决方案有效。但是,当滚动条显示时,内容将滚动到顶部。仍然需要弄清楚如何让内容开始滚动到底部,就像使用“flex-end”一样,希望没有 JS。 (2认同)

lba*_*lic 23

可能你已经解决了这个问题,但我也遇到了这个问题并通过反复试验找到了解决方案,所以我将分享它.

将父容器的显示设置为flex display: flex并将子项与flex-end对齐align-items: flex-end将阻止overflow-y: auto工作.

相反,您可以让您可以使用父容器的下一个CSS属性(在您的情况下session-textchat):

display: flex;
flex-direction: column-reverse; /* 'column' for start, 'column-reverse' for end */
overflow-y: scroll; /* or overflow-y: auto ... */
Run Code Online (Sandbox Code Playgroud)

这将使您的子div显示在父容器的底部(它将起作用flex-end)并在内容高度大于父容器时启用垂直滚动.

如果这听起来令人困惑,我为你做了一个小小的问题: https ://jsfiddle.net/lbartolic/9od4nruy/3/

在jsfiddle中,您可以看到标题部分,内容部分和页脚.容器具有固定的高度,每个部件都需要高度来填充容器._b__content如果内容部分的内容高于_b__content高度,则内容部分将可滚动.

我希望这会对某人有所帮助.干杯.

  • Firefox 50.0.2中的jsfiddle也失败了 - 滚动条显示出来,但它显示为灰色. (4认同)
  • 这似乎只适用于Chrome和Safari(11.0),但不适用于Firefox和IE. (3认同)
  • 最后,唯一正确的答案,而不是说这是一个错误。您没有解决发送方/接收方对齐问题,这可以通过每个类上的简单属性“align-self”来解决。 (2认同)

Ник*_*нов 7

这似乎是浏览器中的常见错误。

您应该将样式分配到 2 个容器上:外部将滚动,内部将是弹性容器。另外,您需要一些 js 来在添加新消息时使消息列表滚动到底部。

这是代码示例:

标记

<div id='outer'>
    <div id='inner-scroll'>
        <div id='inner-flex'>
            <div class='flex-item'>Item 1</div>
            <div class='flex-item'>Item 2</div>
            ...
        </div>
</div>
Run Code Online (Sandbox Code Playgroud)

风格

#inner-scroll {
    height: 100%;
    overflow: auto;
}

#inner-flex {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    min-height: 100%;
}

.flex-item { /*nothing*/ }
Run Code Online (Sandbox Code Playgroud)

JS

function messagePushCallback()
{
    var scrollable=document.getElementById('inner-scroll');
    scrollable.scrollTo(0, scrollable.scrollHeight-scrollable.clientHeight);
}

// for an example
chat.onMessagePush(messagePushCallback);

window.addEventListener('load', messagePushCallback);
Run Code Online (Sandbox Code Playgroud)

在JS中,scrollable.scrollHeight表示元素的整体高度,包括其可见部分之外的空间,而scrollable.clientHeight则为可见部分的高度。

  • 该解决方案适用于 Firefox 57 和 Chrome 63。当前获得最多支持的答案(使用“column-reverse”而不是“flex-end”)似乎不适用于 Firefox。 (2认同)

Sib*_*raj 6

也有另一个解决方案

删除justify-content和添加flex: 1 1 auto;属性到第一个element(create an empty div

的HTML

<div class="content-reversed">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
Run Code Online (Sandbox Code Playgroud)

的CSS

.content-reversed {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
Run Code Online (Sandbox Code Playgroud)

的HTML

<div class="content-reversed">
  <div class="fix"></div> //add this dummy div
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
Run Code Online (Sandbox Code Playgroud)

的CSS

.content-reversed {
  display: flex;
  flex-direction: column;
}
.content-reversed .fix {
   flex: 1 1 auto;
 }
Run Code Online (Sandbox Code Playgroud)