提问者:小点点

在列flexbox上显示溢出滚动条


我试图在我的项目上设置无限滚动,但是我很难使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 */
}

我当前的故障排除步骤:

  • 我已经尝试过这个StackOverflow帖子(滚动内容溢出的flexbox),但没有成功,溢出栏根本不会出现。
  • 如果使用overflow-y:scroll,则只显示停用的滚动条。
  • 元素的大小大于窗口的大小,因此这些元素似乎都显示在显示空间之外。

有人对如何使滚动条出现有想法吗?


共2个答案

匿名用户

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

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

匿名用户

如果子元素的总高度略微超过其父元素的高度,这将是最自然的。因此,您将拥有一个滚动条。

然后,您可能需要在父div上添加滚动事件处理程序:

function loadItems() {
  // check scroll position
  if (scrolledToBottom()) { 
    // fetch more items
  }
}

const container = document.querySelector('.flex-items');
container.addEventListener('scroll', loadItems);