【问题标题】:Displaying overflow scroll bar on a column flexbox在列 flexbox 上显示溢出滚动条
【发布时间】:2022-02-15 23:51:32
【问题描述】:

我正在尝试在我的项目上设置无限滚动,但我很难使 overflow-y: auto; css 属性正常工作。这是我当前的布局:

我正在使用以下 CSS/HTML 代码创建它:

<div class="flex-items">
      <div v-for="game in gamesData.games" class="card-spacing">
        <!-- Contents -->
      </div>
</div>
.flex-items {
  flex: 1;
  display: flex;
  overflow: auto;
  flex-direction: column;
}
.card-spacing {
  margin-bottom: 10px;
  min-height: min-content; /* needs vendor prefixes */
}

我目前的故障排除步骤:

有人知道如何让滚动条出现吗?

【问题讨论】:

标签: html css flexbox


【解决方案1】:

给容器.flex-items一个固定的heightmax-height,只有当容器的内容超过容器的高度时溢出才会起作用

.flex-items {
  flex: 1;
  display: flex;
  height: 500px // specify a height
  min-height: 100%; // take up the full height of its own container
  overflow: auto;
  flex-direction: column;
}

【讨论】:

  • 我本来打算发布一个答案,但你的速度更快,接受我的 :)
  • @ValentinRicard 很乐意为您提供帮助?...感谢您的接受
【解决方案2】:

父容器需要有固定的高度,并且它的子元素的总高度需要超过它。因此,您会在父级上获得一个滚动条。

然后,将滚动事件处理程序添加到父容器以检查用户是否已滚动到底部,如果是,则获取更多项目。

const container = document.querySelector('.flex-items');

function loadItems() {
  if (container.scrollTop === container.scrollTopMax) { 
    // fetch more items
  }
}

container.addEventListener('scroll', loadItems);

【讨论】:

  • 这个问题是在没有任何 JS 代码的情况下发生的,但是你帮我解决了关于总大小的部分哦孩子们。请参阅接受的答案以获取正确的解决方案;)
  • 我认为我的第一句话总结了公认的答案。无论如何,祝你好运:)
猜你喜欢
  • 2018-01-15
  • 2016-05-14
  • 1970-01-01
  • 2011-08-08
  • 2015-10-25
  • 2011-11-21
  • 1970-01-01
  • 2018-10-01
相关资源
最近更新 更多