vue2项目中使用轮播图

1.  npm  i  swiper

2.在组件中引用:

import Swiper from "swiper";

import "swiper/css/swiper.css";

3.在组件中的代码

<template>

  <div class="swiper_box">

    <div class="swiper-container" ref="Swiper">

      <div class="swiper-wrapper">

        <div

          class="swiper-slide"

          v-for="(item, index) in bannerList"

          :key="item.id"

        >

          <img :src="item.imgUrl" />

        </div>

      </div>

      <!-- 如果需要分页器 -->

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

      <!-- 如果需要导航按钮 -->

      <div class="swiper-button-prev"></div>

      <div class="swiper-button-next"></div>

    </div>

  </div>

</template>

<script>

import Swiper from "swiper";

import "swiper/css/swiper.css";

export default {

  name: "Carousel",

  data() {

    return {

      bannerList: [

        {

          id: 1,

          imgUrl:

            "https://img1.baidu.com/it/u=307074048,654359288&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",

        },

        {

          id: 2,

          imgUrl:

            "https://img1.baidu.com/it/u=413643897,2296924942&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",

        },

        {

          id: 3,

          imgUrl:

            "https://img1.baidu.com/it/u=3007048469,3759326707&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",

        },

        {

          id: 4,

          imgUrl:

            "https://img2.baidu.com/it/u=2028084904,3939052004&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",

        },

      ],

    };

  },

  mounted() {

    var mySwiper = new Swiper(this.$refs.Swiper, {

      direction: "horizontal", // 水平切换选项

      loop: true, // 循环模式选项

      // 如果需要分页器

      pagination: {

        el: ".swiper-pagination",

        //分页器类型

        type: "bullets",

        //点击分页器,切换轮播

        clickable: true,

      },

      //自动轮播

      autoplay: {

        delay: 100,

      },

      autoplay: true, //等同于以下设置

      // 如果需要前进后退按钮

      navigation: {

        nextEl: ".swiper-button-next",

        prevEl: ".swiper-button-prev",

      },

    });

  },

};

</script>

<style lang="less" scoped>

.swiper_box {

  width: 100%;

  height: 100%;

  .swiper-container {

    width: 60%;

    height: 40%;

    .swiper-slide {

      width: 100%;

      height: 100%;

      img {

        width: 100%;

        height: 100%;

      }

    }

  }

}

</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值