对比swiper3版本,我觉得swiper4版本更方便一些,所以此文依托的是vue2+swiper4版本(不同版本命令不同,参考官网)。关于swiper7版本我个人是还没有使用。
npm i swiper@4 --save 安装swipernpm uninstall swiper 卸载swiper
html部分结构 一定要严格按照标准来!!
<div class="test-swiper swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="swiper-zoom-container">
<img src="./assets/swiper-images/banner-fade-1.jpg" />
</div>
</div>
<div class="swiper-slide">
<div class="swiper-zoom-container">
<img src="./assets/swiper-images/banner-fade-2.jpg" />
</div>
</div>
</div>
<div class="swiper-button-next">
<i class="iconfont icon-forward"></i>
</div>
<div class="swiper-button-prev">
<i class="iconfont icon-back"></i>
</div>
<div class="swiper-pagination"></div>
</div>
vue部分
<script>
import Swiper from "swiper";
import "swiper/dist/css/swiper.min.css";
import "./assets/swiper-iconfont/iconfont.css";
export default {
name: "App",
mounted() {
var mySwiper = new Swiper(".swiper-container", {
loop: true,
effect: "fade",
autoplay: {
delay: 2000,
disableOnInteraction: false,
},
pagination: {
el: ".swiper-pagination",
clickable: true,
renderBullet: function (index, className) {
return '<span class="' + className + '">' + (index + 1) + "</span>";
},
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
mySwiper.el.onmouseover = function () {
mySwiper.autoplay.stop();
mySwiper.navigation.$nextEl.css("display", "block");
mySwiper.navigation.$prevEl.css("display", "block");
};
mySwiper.el.onmouseleave = function () {
mySwiper.autoplay.start();
mySwiper.navigation.$nextEl.css("display", "none");
mySwiper.navigation.$prevEl.css("display", "none");
};
},
};
</script>
CSS部分(less写法)
.test-swiper {
border: 1px solid red;
margin: 380px auto;
width: 580px;
.swiper-button-next,
.swiper-button-prev {
display: none;
background: rgb(175, 173, 173);
opacity: 0.8;
border-radius: 5px;
text-align: center;
line-height: 2.8em;
.iconfont {
font-size: 30px;
color: white;
}
}
.swiper-pagination {
.swiper-pagination-bullet {
width: 21px;
height: 21px;
}
}
}
|