uniapp 自定义横向滑动列表(金刚区,类似于美团小程序首页顶部)

封装
在这里插入图片描述

<template>
  <view>
    <swiper :indicator-dots="isShowDot && showDot" class="swiper" :style="{height:(145*col)+'rpx'}">
      <swiper-item v-for="(item, index) in listdivInfo" :key="index" class="swiper-item">
        <view v-for="(child, code) in item" class="smallItem" :key="code" :style="{ width: width + '%' }">
          <view class="image">
            <u-image :src="imageURL(child.image)" width="64rpx" height="64rpx" radius="32rpx">
            </u-image>
          </view>
          <view class="name">{{ child.title }}</view>
        </view>
      </swiper-item>
    </swiper>
  </view>
</template>



<script>
export default {
  props: {
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
    //一行排列数
    nums: {
      type: Number,
      default: 4,
    },

    //排列行数
    col: {
      type: Number,
      default: 1,
    },
    //是否展示指示灯
    isShowDot: {
      type: Boolean,
      default: true,
    },
  },

  watch: {
    list: {
      handler: function (newVal, oldVal) {
        this.listdiv();
      },
      deep: true,
    },
  },

  mounted() {
    this.listdiv();
  },
  data() {
    return {
      listdivInfo: [],
      width: 25,
      showDot: true,
    };
  },
  methods: {
    async listdiv() {
      this.width = 100 / this.nums;
      var arr = [];
      let that = this;
      console.log(that.nums * that.col);
      await this.list.forEach((v, index) => {
        var num = Math.floor(index / (that.nums * that.col));
        if (!arr[num]) {
          arr[num] = [];
          arr[num].push(v);
        } else {
          arr[num].push(v);
        }
      });
      this.listdivInfo = arr;
      if (this.listdivInfo.length > 1) {
        this.showDot = true;
      } else {
        this.showDot = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.swiper {
  margin: 8rpx 32rpx;
  background: white;
  border-radius: 32rpx;
}

.swiper-item {
  display: flex;
  flex-wrap: wrap;

  .smallItem {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 16rpx 0;
    overflow: hidden;

    image {
      width: 64rpx;
      height: 64rpx;
    }

    .name {
      margin-top: 8rpx;
      font-size: 16rpx;
    }
  }
}
</style>

使用

<template>
    <view>
        <scrollX :list="cateList" :nums="5" :col="2" />
    </view>
</template>

<script>
import scrollX from "@/components/scroll-x/index.vue";
export default {
    components: {
        scrollX
    },
    data() {
        return {
            cateList: [
                {
                    title: "酒店民宿",
                    image: "图片地址",
                    path: "",
                    isPath: true,
                },
                {
                    title: "健康医疗",
                    image: "",
                    path: "",
                    isPath: true,
                },
            ]
        }
    },
}
</script>


注:可自行修改数据格式

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值