如何在Vue Material中设置灵活的网格

IT技术 javascript vue.js flexbox material-design vue-material
2021-02-26 21:53:02

我正在尝试构建一个界面,该界面使用 Vue-Material 在网格中呈现用户输入的卡片。卡片渲染正确。但是,我希望我的网格以消除间隙的方式弯曲、对齐和交错不同大小的卡片,如下所示:

在此处输入图片说明

下面的代码对应上面的网格:

<template>
  <div class="content">
    <div class="md-layout">
      <div
        v-for="post in posts.slice().reverse()" :key="post.id"
        class="md-layout-item md-size-20 md-xsmall-size-100"
      >
        <md-card>
          <md-card-content v-if="post.downloadUrl">
            <md-card-media>
              <img :src="post.downloadUrl" style="width: 100%"/>
            </md-card-media><br>
            <p>{{ post.content }}</p>
            <p>{{ post.timestamp | moment }}</p>
          </md-card-content>

          <md-card-content v-else>
            <p>{{ post.content }}</p>
            <p>{{ post.timestamp | moment }}</p>
          </md-card-content>
          <md-card-actions>
            <md-button class="md-icon-button md-info">
              <md-icon>favorite</md-icon>
            </md-button>
            <md-button class="md-icon-button md-info">
              <md-icon>share</md-icon>
            </md-button>
          </md-card-actions>
        </md-card>
      </div>
    </div>
  </div>
</template>

如何配置此 Vue-Material 布局以消除这些间隙的方式排列卡片?谢谢!

在此处输入图片说明

示例#3

在此处输入图片说明

3个回答

您可以使用网格布局:

Vue.use(VueMaterial.default)

new Vue({
  el: '#app'
})
.grid-container {
  display: grid;
  max-width: 100%;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(10px, auto);
  grid-gap: 1rem;
  padding: 1rem;
}
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1,minimal-ui" name="viewport">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons">
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/vue-material.min.css">
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/theme/default.css">

<body>
  <div id="app">
    <div class="grid-container">
      <md-card>
        <md-card-media>
          <img src="https://vuematerial.io/assets/examples/card-image-1.jpg" alt="People">
        </md-card-media>
        <md-card-header>
          <div class="md-title">Card without hover effect</div>
        </md-card-header>

        <md-card-content>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
        </md-card-content>

        <md-card-actions>
          <md-button>Action</md-button>
          <md-button>Action</md-button>
        </md-card-actions>
      </md-card>

      <md-card md-with-hover>
        <md-ripple>
          <md-card-header>
            <div class="md-title">Card with hover effect</div>
            <div class="md-subhead">It also have a ripple</div>
          </md-card-header>

          <md-card-content>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
          </md-card-content>

          <md-card-actions>
            <md-button>Action</md-button>
            <md-button>Action</md-button>
          </md-card-actions>
        </md-ripple>
      </md-card>
      <md-card>
        <md-card-header>
          <div class="md-title">Card without hover effect</div>
        </md-card-header>

        <md-card-content>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
        </md-card-content>

        <md-card-actions>
          <md-button>Action</md-button>
          <md-button>Action</md-button>
        </md-card-actions>
      </md-card>

      <md-card md-with-hover>
        <md-ripple>
          <md-card-header>
            <div class="md-title">Card with hover effect</div>
            <div class="md-subhead">It also have a ripple</div>
          </md-card-header>

          <md-card-content>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
          </md-card-content>

          <md-card-actions>
            <md-button>Action</md-button>
            <md-button>Action</md-button>
          </md-card-actions>
        </md-ripple>
      </md-card>
      <md-card>
        <md-card-header>
          <div class="md-title">Card without hover effect</div>
        </md-card-header>

        <md-card-content>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
        </md-card-content>

        <md-card-actions>
          <md-button>Action</md-button>
          <md-button>Action</md-button>
        </md-card-actions>
      </md-card>

      <md-card md-with-hover>
        <md-ripple>
          <md-card-header>
            <div class="md-title">Card with hover effect</div>
            <div class="md-subhead">It also have a ripple</div>
          </md-card-header>

          <md-card-content>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
          </md-card-content>

          <md-card-actions>
            <md-button>Action</md-button>
            <md-button>Action</md-button>
          </md-card-actions>
        </md-ripple>
      </md-card>
      <md-card>
        <md-card-header>
          <div class="md-title">Card without hover effect</div>
        </md-card-header>

        <md-card-content>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
        </md-card-content>

        <md-card-actions>
          <md-button>Action</md-button>
          <md-button>Action</md-button>
        </md-card-actions>
      </md-card>

      <md-card md-with-hover>
        <md-ripple>
          <md-card-header>
            <div class="md-title">Card with hover effect</div>
            <div class="md-subhead">It also have a ripple</div>
          </md-card-header>

          <md-card-content>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
          </md-card-content>

          <md-card-actions>
            <md-button>Action</md-button>
            <md-button>Action</md-button>
          </md-card-actions>
        </md-ripple>
      </md-card>
    </div>
    &copy Image linked from https://vuematerial.io/
  </div>

  <script src="https://unpkg.com/vue"></script>
  <script src="https://unpkg.com/vue-material"></script>

更多关于CSS 网格https : //developer.mozilla.org/en-US/docs/Web/CSS/grid

另一种解决方案

你可以用

Andy Barefoot 的解决方案:https ://medium.com/@andybarefoot/a-masonry-style-layout-using-css-grid-8c663d355ebb(Codepen:https : //codepen.io/andybarefoot/pen/QMeZda ),

或任何来自这里:https : //css-tricks.com/piecing-together-approaches-for-a-css-masonry-layout/

不幸的是,我无法将其添加为片段 - 内容太长,所以我的答案将超过 30,000 个字符。

是的!砌体风格。如何在上述模板中实现这一点?
2021-04-16 21:53:02
哦,Pinterest(砖石)风格?
2021-04-20 21:53:02
@JS_is_awesome18 我添加了另一个解决方案 - 肯定有效;您需要自定义它(类、JS 对象等)
2021-04-20 21:53:02
有点,但我使用 v-for 指令循环遍历所有卡片,然后按照帖子编号的顺序将它们返回到屏幕(参见图片)。卡片将代表不同的大小。我希望卡片以类似于上面的图像示例 #3 的方式呈现,在一个漂亮的、交错的网格中,没有间隙。我怎样才能做到这一点?
2021-05-09 21:53:02
意思是,如何将砌体应用于每个循环卡片项目?当长卡位于短卡下方时(见上图),如何将它们配置为确认砖石布局而不会在行和列中产生间隙?
2021-05-11 21:53:02

感谢今天所有回复的人。但是,我需要的是这样的插件:https : //www.npmjs.com/package/vue-masonry-css

我需要做的就是将 v-for 循环容器和卡片嵌入到以下内容中:

<masonry
  :cols="3"
  :gutter="30"
  >
  <div v-for="(item, index) in items" :key="index">Item: {{index + 1}}</div>
</masonry>

这就是我试图实现的目标:

在此处输入图片说明

你可以使用vue-masonry插件。

npm i vue-masonry

看到它工作沙箱在这里

谢谢,哈哈,我实际上在你发布这个之前就想到了这一点。请看我下面的回答。不过我真的很感激!
2021-05-07 21:53:02
不过,这不是同一个插件。我建议的那个有介绍和调整大小的动画。但我想你最清楚你想要/需要什么。干杯! 通常我会删除我的答案,但由于它与您的不完全相同,其他有类似问题的人可能会发现它很有用。
2021-05-08 21:53:02