HomeBanner.vue 2.25 KB
<template>
  <section class="home-banner">
    <van-swipe
      class="banner-swiper"
      :autoplay="autoplay"
      lazy-render
      @change="handleChange"
    >
      <van-swipe-item
        class="swiper-item"
        v-for="(item, idx) in banners"
        :key="idx"
      >
        <img
          v-if="item.picUrl"
          class="banner-img"
          :src="item.picUrl"
          @click="handleItem(item)"
        />
        <div v-else class="banner-img white">
          <div class="banner-img skeleton"></div>
        </div>
      </van-swipe-item>

      <template #indicator>
        <div class="nafmii-indicator">
          <span
            class="indicator-item"
            :class="{ 'indicator-active': active == idx }"
            v-for="(item, idx) in banners"
            :key="idx"
          />
        </div>
      </template>
    </van-swipe>
  </section>
</template>

<script>
export default {
  name: 'HomeBanner',
  props: {
    data: {
      type: Object,
      default: () => { return {} }
    }
  },
  data () {
    return {
      active: 0,
    }
  },
  computed: {
    autoplay () {
      const { carouseTime } = this.data || {}
      // 默认5秒
      return carouseTime && (carouseTime * 1000) || 5000
    },
    banners () {
      const { carouseVOList } = this.data || {}
      return carouseVOList || [{}, {}]
    }
  },
  methods: {
    handleItem (banner) {
      const { linkUrl, linkType } = banner
    },
    handleChange (index) {
      this.active = index
    }
  }
}
</script>

<style lang="less" scoped>
.white {
  background: #eee;
}
@PaddingSize: 15;
.home-banner {
  margin-top: @PaddingSize;
  .banner-swiper {
    .banner-img {
      height: 145px;
      border-radius: 5px;
      width: 100%;
    }
    @PointSize: 5px;
    .nafmii-indicator {
      position: absolute;
      left: 0;
      bottom: 10px;
      right: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      height: @PointSize;
      .indicator-item {
        background: rgba(255, 255, 255, 0.4);
        width: @PointSize;
        height: @PointSize;
        border-radius: calc(@PointSize / 2);
        margin: 0 4px;

        &.indicator-active {
          background: white;
          width: 21.5px;
        }
      }
    }
  }
}
</style>