Vue3 瀑布流 组件(vue2修改部分代码及可适用)

Vue3 瀑布流 组件(vue2修改部分代码及可适用)

暂未做图片瀑布流的适配。 后续可能会增加。
不多说直接上代码!!!

<template>
  <div class="waterfall-row" ref="waterfallRow">
    <template v-for="(item, index) of list" :key="item">
      <div class="waterfall-col">
        <div class="waterfall-card">
          <slot :item="item" :index="index"></slot>
        </div>
      </div>
    </template>
  </div>
</template>
<script lang="ts" setup>
import { nextTick, onMounted, reactive, ref } from "vue";

interface waterfallFlow {
  waterfallFlowHeight: Array<number>;
  left: number;
  rowHeight: number;
}
const props = defineProps({
  list: {
    type: Object,
    default: () => ({}),
  },
  columns: {  // 一共几列,默认为2
    type: Number,
    default: 2,
  },
  columnGap: {  // 列与列之间的间距,最好设置为双数
    type: Number,
    default: 10,
  },
  rowGap: {		// 行与行之间的间距,最好设置为双数
    type: Number,
    default: 10,
  },
});
const state: waterfallFlow = reactive({
  waterfallFlowHeight: new Array(props.columns).fill(0),
  left: Math.floor(100 / props.columns),
  rowHeight: 0,
});
const waterfallRow = ref();
const waterfallFlowFun = () => {
  const dom = waterfallRow.value.querySelectorAll(".waterfall-col");
  dom.forEach((item: any) => {
    item.style.position = "absolute";
    const minIndex = filterMin();
    item.style.left = `calc(${minIndex * state.left}% + ${(props.columnGap / 2) * minIndex}px)`;
    const itemColumnGap = props.columnGap * (state.waterfallFlowHeight[minIndex] == 0 ? 0 : 1);
    item.style.top = state.waterfallFlowHeight[minIndex] + itemColumnGap + "px";
    state.waterfallFlowHeight[minIndex] += item.querySelector(".waterfall-card").offsetHeight + itemColumnGap;
  });
  state.rowHeight = getrowHeight();
};
const getrowHeight = () => Math.max.apply(null, state.waterfallFlowHeight);
const filterMin = () => {
  const min = Math.min.apply(null, state.waterfallFlowHeight);
  return state.waterfallFlowHeight.indexOf(min);
};

onMounted(() => {
  nextTick(() => {
    waterfallFlowFun();
  });
});
</script>

<style lang="scss" scoped>
$columnGap: v-bind("props.columnGap");
$columnWidth: v-bind("state.left");
$rowHeight: v-bind("state.rowHeight");
.waterfall-row {
  position: relative;
  min-height: calc($rowHeight * 1px);
  display: flex;
  flex-wrap: wrap;
  .waterfall-col {
    width: calc($columnWidth * 1% - $columnGap * 1px / 2);
    .waterfall-card {
      padding-left: 0 !important;
      padding-right: 0 !important;
      margin-top: 0 !important;
    }
  }
}
</style>

1、创建 组件 直接复制代码写入即可
2、直接引入 vue 组件直接使用即可

<water-fall :list="otherContacts">
  <template v-slot="slotProps">
    你要写入的内容! slotProps = {item:{},index}
  </template>
</water-fall>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天空Kong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值