如何水平渲染项目(Masonry Layout)?

How to render the item horizontally (Masonry Layout )?

我想在页面加载时水平呈现项目,请参考以下图片

<section
        tabindex="-1"
        class="relative mx-8 mt-10 mb-20 max-w-7xl focus:outline-none sm:mx-16 md:mx-20 lg:mx-24 xl:mx-auto"
    >
        <ul
            class="h-full w-full list-none columns-1 gap-4 space-y-12 overflow-hidden pb-32 md:columns-2 lg:columns-3 lg:gap-8 xl:columns-4"
        >
            <ExploreCard
                v-for="(post, index) in posts.data"
                :key="index"
                :post="post"
                :canLike="this.canLike"
            />
        </ul>       
</section>

Currently

Expected

像这样,
https://reactjsexample.com/rendering-columns-from-a-list-of-children-with-horizontal-ordering/

I am looking for a Js, Vue, or CSS solution.

我建议您使用网格的 tailwind class 实用程序来创建网格容器: https://tailwindcss.com/docs/display#:~:text=Use%20grid%20to%20create%20a%20grid%20container.

我最终得到了一个包解决方案,它工作得很好!

DaPotatoMan/vue-next-masonry

这就是它的样子

<masonry
            :cols="{ default: 4, 1024: 3, 768: 2, 640: 1 }"
            :gutter="{ default: 40, 1024: 30, 768: 20 }"
        >
            <div v-for="(post, index) in posts.data" :key="index" class="mb-10">
                <ExploreCard
                    v-for="(post, index) in posts.data"
                    :key="index"
                    :post="post"
                    :canLike="this.canLike"
                />
            </div>
</masonry>

这样,所有的post都按水平顺序渲染了!

这是来自@haltersweb

纯CSS的回答

CSS-only masonry layout

显示:inline-block

ul {
    margin-left: .25em;
    padding-left: 0;
    list-style: none;
}
li {
    margin-left: 0;
    padding-left: 0;
    display: inline-block;
    width: 30%;
    vertical-align: top;
}

<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li>item 4</li>
  <li>item 5</li>
  <li>item 6</li>
  <li>item 7</li>
  <li>item 8</li>
  <li>item 9</li>
</ul>

显示:弹性

ul {
    margin-left: .25em;
    padding-left: 0;
    list-style: none;
    display: flex;
    flex-wrap: wrap;
}
li {
    margin-left: 0;
    padding-left: 0;
    width: 33.3%;
}

<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li>item 4</li>
  <li>item 5</li>
  <li>item 6</li>
  <li>item 7</li>
  <li>item 8</li>
  <li>item 9</li>
</ul>