📖vue-awesome-swiper 轮播图功能
前言
vue-awesome-swiper 是vue-cli中的一个轮播图功能插件,通过安装即可使用
代码
首先需要安装vue-awesome-swiper,这里我们安装的是 vue-awesome-swiper 3.1.3 版本
npm install vue-awesome-swiper@3.1.3 --save
<template> <div class="images_li"> <swiper :options="swiperOption" ref="mySwiper"> <swiper-slide v-for='item in imglist' :key='item.id'><img :src="item" class="swiperImg"></swiper-slide> <div class="swiper-pagination" slot="pagination"></div> <div class="swiper-button-prev" slot="button-prev"></div> <div class="swiper-button-next" slot="button-next"></div> </swiper> </div> </template> <script> // 组件中引入插件 import { swiper, swiperSlide } from "vue-awesome-swiper"; import "swiper/dist/css/swiper.css"; export default { name:"images_li", components: { swiper, swiperSlide }, data() { return { swiperOption: { loop: true, autoplay: { delay: 3000, stopOnLastSlide: false, disableOnInteraction: false }, // 显示分页 pagination: { el: ".swiper-pagination", clickable: true //允许分页点击跳转 }, // 设置点击箭头 navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev" } }, imglist:[require('@/assets/image/swiper.png'),require('@/assets/image/swiper2.png'),require('@/assets/image/swiper3.png'),require('@/assets/image/swiper1.png')]//图片地址 }; }, computed: { swiper() { return this.$refs.mySwiper.swiper; } }, mounted() { // current swiper instance // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了 console.log("this is current swiper instance object", this.swiper); // this.swiper.slideTo(3, 1000, false); } }; </script> <style scoped > .recommendPage .swiper-container{ position: relative; width: 100%; height: 200px; background: pink; } .recommendPage .swiper-container .swiper-slide{ width: 100%; line-height: 200px; background: yellowgreen; color: #000; font-size: 16px; text-align: center; } .swiperImg{max-width: 100%;height: 500px;} </style>