因为随着swiper的版本更新,各种版本出现的写法有所不同
npm install swiper@3 --save-dev
import 'swiper/dist/css/swiper.min.css'
import 'swiper/dist/js/swiper.min'
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1div>
<div class="swiper-slide">Slide 2div>
<div class="swiper-slide">Slide 3div>
div>
<div class="swiper-pagination">div>
<div class="swiper-button-prev">div>
<div class="swiper-button-next">div>
<div class="swiper-scrollbar">div>
div>
之后在我们的mounted生命周期里面
new Swiper(".swiper-container", {
//自动轮播,这里直接写毫秒就可以了
autoplay: 3000,
//设置环路
loop: true,
// 如果需要分页器
pagination: ".swiper-pagination",
// 如果需要前进后退按钮
nextButton: ".swiper-button-next",
prevButton: ".swiper-button-prev",
// 如果需要滚动条
scrollbar: ".swiper-scrollbar",
});
样式
.swiper-container {
height: 500px;
width: 100%;
.swiper-wrapper {
.swiper-slide {
width: 100%;
height: 100%;
background-color: #42b983;
text-align: center;
line-height: 500px;
}
}
}
完成这些swiper3的基本用法就全了
npm install swiper vue-awesome-swiper --save
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper)
引入之后我们就可以直接写dom
<swiper ref="mySwiper" :options="swiperOptions">
<swiper-slide>Slide 1swiper-slide>
<swiper-slide>Slide 2swiper-slide>
<swiper-slide>Slide 3swiper-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>
<div class="swiper-scrollbar" slot="scrollbar">div>
swiper>
然后data函数里面return一个
swiperOptions: {
autoplay:true,
//设置环路
loop: true,
// 如果需要分页器
pagination: {
el: ".swiper-pagination",
},
// 如果需要前进后退按钮
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
// 如果需要滚动条
scrollbar: {
el: ".swiper-scrollbar",
},
}
然后计算函数computed需要写一个方法
swiper() {
return this.$refs.mySwiper.$swiper;
}
npm i swiper
import { Navigation, Pagination, Scrollbar, A11y,Autoplay } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue/swiper-vue.js";
import "swiper/swiper.min.css";
import "swiper/modules/navigation/navigation.min.css";
import "swiper/modules/pagination/pagination.min.css";
import "swiper/modules/scrollbar/scrollbar.min.css";
import "swiper/modules/autoplay/autoplay.min.css";
components:{
Swiper,
SwiperSlide
}
setup里面写
setup() {
//获取swiper对象
const onSwiper = (swiper) => {
console.log(swiper);
};
//轮播图改变触发的事件
const onSlideChange = () => {
console.log("slide change");
};
return {
onSwiper,
onSlideChange,
//抛出各种样式
modules: [Navigation, Pagination, Scrollbar, A11y,Autoplay],
};
};
在dom上是这样写的
<swiper
:modules="modules"
:slides-per-view="1"
:space-between="50"
navigation
:pagination="{ clickable: true }"
:scrollbar="{ draggable: true }"
:autoplay="{Autoplay:true}"
@swiper="onSwiper"
@slideChange="onSlideChange"
>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
.swiper-slide {
height: 300px;
line-height: 300px;
font-size: 30px;
text-align: center;
background-color: pink;
}
0条评论