当前位置: 首页 > 工具软件 > vue-waterfall > 使用案例 >

vue实现简单瀑布流布局(vue-waterfall2)

韦阳晖
2023-12-01

1、安装vue-waterfall2

 npm i vue-waterfall2@latest --save
 或者
  yarn add vue-waterfall2@latest --save

2、main.js中全局使用
注意:如果不在main.js全局引入,而在某个页面import引入的话,会报render渲染错误

import Vue from 'vue'
import App from './App.vue'
import router from "@/router";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import water from "vue-waterfall2";

Vue.use(water);
Vue.use(ElementUI);

Vue.config.productionTip = false
new Vue({
    router,
    el: '#app',
    render: h => h(App)
});

3、创建组件

<template>
  <div>
    <waterfall :col="col" :data="data">
      <!--  @loadmore="loadmore"
          @scroll="scroll" -->
      <template>
        <div class="cell-item" v-for="(item, index) in data" :key="index">
          <img :src="item.imgSrc" alt="加载错误" />
          <div class="item-body">
            <div class="item-desc">{{ item.title }}</div>
            <div class="item-footer">
              <div class="footer-left">
                <img :src="item.avatar" alt="" srcset="" />
                <div class="name">{{ item.user }}</div>
              </div>
              <div class="like">
                <img src="../../assets/images/icon/vote.png" alt="" />
                <div class="like-total">{{ item.vote_num }}</div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </waterfall>
  </div>
</template>
<script>
export default {
  props: {
    data: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      col: 5,
    };
  },
  methods: {},
};
</script>
<style lang="scss" scoped>
.item-body {
  margin: 9px;
  .item-desc {
    text-align: left;
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 14px;
    line-height: 16px;
    color: #000000;
  }
  .item-footer {
    display: flex;
    justify-content: space-between;
    .footer-left {
      display: flex;
      align-items: center;
      font-family: SF Pro Display;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 14px;
      color: rgba(0, 0, 0, 0.6);
      img {
        border-radius: 50%;
        width: 22px;
        height: 22px;
        margin-right: 4px;
      }
    }
    .like {
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: SF Pro Display;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 14px;
      color: rgba(0, 0, 0, 0.4);
      img{
          margin-right: 4px;
      }
    }
  }
}
.cell-item {
  width: 100%;
  margin-bottom: 6px;
  background: #ffffff;
  border: 2px solid #f0f0f0;
  border-radius: 6px;
  overflow: hidden;
  box-sizing: border-box;
  img {
    width: 100%;
    height: auto;
    display: block;
  }
}
</style>

4、在页面中引入组件

<template>
  <div class="container">
      <div class="waterfall">
        <my-waterfall :data="cardList"></my-waterfall>
      </div>
   </div>
</template>
<script>
import myWaterfall from "@/components/myWaterfall";
export default {
  components: { myWaterfall },
  data() {
    return {
      cardList: [
        {
          imgSrc: require("../../../mock/image/waterfall/01.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Noice cancelling is a secret weapon",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/02.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Work from home is so boring",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/05.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Go out for the good sunset",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/03.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Let’s run together",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/02.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Work from home is so boring",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/04.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/06.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "New Sneaker has just arrived",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/08.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Good smart charger ",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/07.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "Good smart charger ",
        },
        {
          imgSrc: require("../../../mock/image/waterfall/04.png"),
          avatar: require("../../assets/images/avator.png"),
          user: "Lewis",
          vote_num: 999,
          title: "",
        },
      ],
    };
  },
};
</script>
<style lang="scss" scoped>
.container {
  background-color: #f8f8f8;
  width: 100%;
}
</style>

 类似资料: