前端项目经验 —— vue项目中使用vue-masonry插件实现瀑布流

 npm下载vue-masonry插件:

npm i vue-masonry

 在mian.js中引入插件:

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";

// 瀑布流插件
import { VueMasonryPlugin } from "vue-masonry";

const app = createApp(App);
app.use(VueMasonryPlugin);
app.mount("#app");

 使用方式:

<script setup>
// 实现随机颜色
const getRandomColor = () => {
  var r = Math.floor(Math.random() * 256); // 生成 0~255 之间的随机数
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  return `rgb(${r}, ${g}, ${b})`; // 拼接 RGB 字符串
};
</script>

<template>
  <div v-masonry class="container">
    <div
      v-masonry-tile
      v-for="(item, index) in 10"
      :class="['item']"
      :style="{
        height: parseInt(Math.random() * 200) + 'px',
        background: getRandomColor(),
      }"
      :key="index"
    >
      {{ index }}
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 80vw;
  box-sizing: border-box;

  > .item {
    min-height: 80px;
    width: 20%;
    margin-bottom: 10px;
    margin-left: 10px;
  }
}
</style>

效果展示:

注意事项:

使用该插件会有一定的兼容问题,在之前一个项目中,我在刚开始使用时不会有任何问题,但是后面的一次打包过程中报错,并且找了各种方式还是无法解决,所以无奈只能去掉这个瀑布流方案。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值