vue-awesome-swiper 简单轮播图

先安装 npm install swiper vue-awesome-swiper --save
这里使用的swiper使用的是swiper5.x版本   使用6.x分页器不显示,很多配置都没有效果
使用全局引入 

import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper)

局部引入
import {Swiper, SwiperSlide} from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

 

界面

复制代码
<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide v-for="(item,index) of banners" :key="index">
      <!--  loop为true的情况下不能使用vue-loadload加载图片会出现问题 -->
      <img :src="item.icon" ref="icon">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<style scoped>
  .swiper-slide {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  .swiper-slide img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }

  .swiper-container {
    /*这里需要设置宽高*/
    /*--swiper-theme-color: #ff6600;*/
    --swiper-pagination-color: #00ff33; /* 两种都可以 */
  }
</style>
复制代码

 

 

js代码

复制代码
//import {Swiper, SwiperSlide} from 'vue-awesome-swiper'
  //import 'swiper/css/swiper.css'

  export default {
    name: "Banner",
    // components: {
    //   Swiper,
    //   SwiperSlide
    // },
    computed: {
      swiper() {
        return this.$refs.mySwiper.$swiper
      }
    },
    props: {
      banners: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {
        swiperOptions: {
          pagination: {
            el: '.swiper-pagination',
            clickable: true,
            type: 'bullets'
          },
          init: true,
          loop: true,
          initialSlide: 0,
          autoplay: {
            delay: 5000,
            disableOnInteraction: false
          },
          speed: 800,
          direction: "horizontal",
          on: {
            click: () => {
              //this.swiper.realIndex 选中的索引(对应我们传入的banners集合)
              //loop为true时轮播图前后会添加滑动时的占位图片
              //所以会多出2张图片 这里的索引就和数据对应不上了
              //所以需要使用realInedx
              //this.swiper.activeIndex 选中的索引(这里是当前真实dom的索引)
              this.$emit('itemClick',this.swiper.realIndex)
            }
          }
        }
      }
    },
    methods: {
    },
    mounted() {
    }
  }
复制代码

 

posted @   荣超  阅读(555)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示