vue如何解決輪播圖(Swiper)第一張圖片一閃而過問題
vue輪播圖(Swiper)第一張圖片一閃而過
解決方式
初始化(Swiper)時(shí),外層添加一個(gè)定時(shí)器
代碼:
setTimeout(function() {
var mySwiper = new Swiper($(el), {
autoplay: true,//可選選項(xiàng),自動(dòng)滑動(dòng)
speed:500,//切換時(shí)間
loop : true,
pagination : pagination,
paginationType : paginationType,
// observer:true,
// observeParents:true,
// onSlideChangeStart: function(swiper){
// swiper.update();
// swiper.reLoop();
// },
prevButton: $(el).find('.swiper-button-prev'),
nextButton: $(el).find('.swiper-button-next'),
})
}, 500)
vue圖片輪播實(shí)現(xiàn)過程
效果圖:

由于沒有素材就隨便找了123456來代替選中的圓點(diǎn)。
實(shí)現(xiàn)思路
首先綁定數(shù)據(jù)源,循環(huán)出每個(gè)圖片,在通過isShow字段來判斷是否顯示圖片。在圖片元素寫這兩個(gè) v-bind:src="item.src" v-show="item.isShow" 一個(gè)src用來顯示圖片,show用來判斷圖片是否顯示。
再為123456下面這個(gè)導(dǎo)航添加點(diǎn)擊事件,通過點(diǎn)擊的元素來設(shè)置該數(shù)據(jù)顯示出圖片,同時(shí)其他圖片隱藏。
然后在vue created方法調(diào)用開始循環(huán)事件,來實(shí)現(xiàn)圖片輪播。并用一個(gè)屬性記錄起來,方便后面停止循環(huán)。
開啟輪播后,判斷當(dāng)前顯示的圖片是否是最后一張如果是就從1開始,如果不是則下標(biāo)加+1去顯示下一張圖片。
全部代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue輪播圖</title>
</head>
<script src="vue.min.js"></script>
<script src="clipboard.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
<style type="text/css">
#test{text-align: center;margin:0 auto;}
.baner{}
.baner img{width:700px;height: 500px;}
.num{margin-top: 20px}
.num a{color: black;}
.num a span{ margin-left: 30px;font-size: 20px; text-decoration:none;}
/*.num a:hover{color: red;}*/
.isSelect {color: red;}
</style>
<body>
<div id="test">
<h1>vue輪播圖</h1>
<div class="baner">
<div class="banerimg" v-for="item in dataList">
<img v-bind:src="item.src" v-show="item.isShow">
</div>
<div class="num" @mouseover="focusImg()" @mouseout="startInterval">
<a href="javascript:void(0)" v-for="item in dataList" @click="changeImg(item.seq)">
<span :class={'isSelect':item.isShow}>{{item.seq}}</span>
</a>
</div>
</div>
</div>
</body>
<script >
new Vue({
el : "#test",
data: {
interval:'',
dataList:[
{name:'1',src:'banerSroll1.jpg',isShow:true,seq:1},
{name:'2',src:'banerSroll2.jpg',isShow:false,seq:2},
{name:'3',src:'banerSroll3.jpg',isShow:false,seq:3},
{name:'4',src:'banerSroll4.jpg',isShow:false,seq:4},
{name:'5',src:'banerSroll5.jpg',isShow:false,seq:5},
{name:'6',src:'banerSroll6.jpg',isShow:false,seq:6},
],
},
created:function(){
this.startInterval();
},
methods:{
changeImg :function(seq){
var newData = this.dataList;
for (var i = 0;i <newData.length; i++) {
if(newData[i].seq==seq)
newData[i].isShow=true;
else
newData[i].isShow=false;
}
this.dataList = newData;
},
startInterval:function(){
let that = this;
this.interval = setInterval(function(){
that.scollImg();
},1000)
},
scollImg:function(){
var newData = this.dataList.filter(function(val){return val.isShow})[0];
if(newData.seq==this.dataList.length){
this.changeImg(1);
}else{
this.changeImg(newData.seq+1);
}
},
focusImg :function(){
clearInterval(this.interval);
}
},
computed :function(){
}
})
</script>
</html>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue路由組件路徑如何用變量形式動(dòng)態(tài)引入
這篇文章主要介紹了vue路由組件路徑如何用變量形式動(dòng)態(tài)引入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue 使用v-model實(shí)現(xiàn)控制子組件顯隱效果
v-model 可以實(shí)現(xiàn)雙向綁定的效果,允許父組件控制子組件的顯示/隱藏,同時(shí)允許子組件自己控制自身的顯示/隱藏,本文給大介紹Vue 使用v-model實(shí)現(xiàn)控制子組件顯隱,感興趣的朋友一起看看吧2023-11-11
vue?elementUi中的tabs標(biāo)簽頁使用教程
Tabs 組件提供了選項(xiàng)卡功能,默認(rèn)選中第一個(gè)標(biāo)簽頁,下面這篇文章主要給大家介紹了關(guān)于vue?elementUi中的tabs標(biāo)簽頁使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
詳解vue之自行實(shí)現(xiàn)派發(fā)與廣播(dispatch與broadcast)
這篇文章主要介紹了詳解vue之自行實(shí)現(xiàn)派發(fā)與廣播(dispatch與broadcast),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

