swiper轮播图,抽离公用组件vue2(本项目版本为9.1.1)

代码如下,可以直接复制,单独组价引用,因本项目同一页面需要多次使用,故此id需要单独抽离,采用传值方法解决不同id问题

引用案例:

 <CarouselAssembly

            :idb="'swiperContainer8'"

            :slides="slides"

            :display-number="3"

          ></CarouselAssembly>

展示五张

HTML代码如下:

<template>

  <div class="about">

    <div :id="idb" class="swiper-container">

      <div class="swiper-wrapper">

        <div v-for="(item, index) in slides" :key="index" class="swiper-slide">

          <div style="width: 80px; height: 136px" class="isbox" @click="openDetails(item)"

            ><p>暂无图片</p></div

          >

        </div>

      </div>

      <!-- <div class="swiper-pagination"></div> -->

      <div

        slot="button-prev"

        class="swiper-button-prev"

        style="transform: scale(0.5); width: calc(var(--swiper-navigation-size) / 44 * 0)"

        @click="prevBtn"

      ></div>

      <div

        slot="button-next"

        class="swiper-button-next"

        style="transform: scale(0.5); width: calc(var(--swiper-navigation-size) / 44 * 0)"

        @click="nextBtn"

      ></div>

    </div>

  </div>

</template>

  js代码如下:

<script>

import Swiper from "swiper";

import "swiper/swiper.min.css";

export default {

  name: "About",

  props: {

//需要轮播的图片列表

    slides: {

      type: Array,

      default: () => [],

    },

//需要一次性展示的张数,可以自己传

    displayNumber: {

      type: Number,

      default: 3,

    },

//同一页面多次复用,需要使用不同的id

    idb: {

      type: String,

      default: "",

    },

  },

  data() {

    return {

      initidid: null,

    };

  },

  mounted() {

    this.$nextTick(() => {

      this.initidid = new Swiper("#" + this.idb, {

        slidesPerView: this.displayNumber,

        spaceBetween: 0,

        centeredSlides: true,

        loop: true,

        pagination: {

          el: ".swiper-pagination",

          clickable: true,

        },

        navigation: {

          nextEl: ".swiper-button-next",

          prevEl: ".swiper-button-prev",

        },



        // 如果需要分页器

        // pagination: {

        //   el: ".swiper-pagination",

        // },



        // 如果需要滚动条

        // scrollbar: '.swiper-scrollbar',

        //如果需要自动切换海报

        // autoplay: {

        //   delay: 1000,//时间 毫秒

        //   disableOnInteraction: false,//用户操作之后是否停止自动轮播默认true

        // },

      });

    });

  },

  methods: {

    prevBtn() {

      console.log(this.initidid);

      this.initidid.slidePrev();

    },

    nextBtn() {

      this.initidid.slideNext();

    },

    openDetails(item) {

      this.$emit("openPresent", item);

    },

  },

};

</script>

css代码如下:

<style lang="less" scoped>

.about {

  overflow: hidden;

  height: 150px;

  position: relative;

}

.swiper-container {

  width: 100%;

  height: 100%;

}

.swiper-slide {

  text-align: center;

  font-size: 18px;

  background: transparent;



  /* Center slide text vertically */

  display: -webkit-box;

  display: -ms-flexbox;

  display: -webkit-flex;

  display: flex;

  -webkit-box-pack: center;

  -ms-flex-pack: center;

  -webkit-justify-content: center;

  justify-content: center;

  -webkit-box-align: center;

  -ms-flex-align: center;

  -webkit-align-items: center;

  align-items: center;

  transition: 300ms;

  transform: scale(0.8);

  .isbox {

    background: url(~@/assets/images/new-icon04.png) no-repeat;

    background-size: 100% 100%;

    p {

      width: 33px;

      height: 33px;

      font-size: 12px;

      text-shadow: 0 0 4px #0091ff;

      color: #fff;

      margin: auto;

      line-height: 2 !important;

      margin-top: 44px;

      word-wrap: break-word;

    }

  }

}

.swiper-slide-active,

.swiper-slide-duplicate-active {

  transform: scale(1);

  .isbox {

    background: url(~@/assets/images/new-icon02.png) no-repeat;

    background-size: 100% 100%;

  }

}

</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值