vue利用vant組件實(shí)現(xiàn)輪播圖效果
1.引言
在互聯(lián)網(wǎng)日漸內(nèi)卷的情況下,越來(lái)越注重用戶體驗(yàn),輪播圖的應(yīng)用場(chǎng)景越來(lái)越 廣泛,如應(yīng)用于以下場(chǎng)景
- 商品詳情頁(yè) 。在商品詳情頁(yè)中展示足夠的商品信息,可以將輪播圖比作一個(gè)團(tuán)隊(duì),每個(gè)團(tuán)隊(duì)成員都有自己的工作,例如首圖負(fù)責(zé)吸引流量,其余的負(fù)責(zé)商品的賣點(diǎn)、促銷信息、購(gòu)物保障等等。
- 商品賣點(diǎn)展示 。在輪播圖中靠前位置展示買家最看重的商品賣點(diǎn),可以減少買家的購(gòu)買決策時(shí)長(zhǎng)。例如,如果商品在做促銷活動(dòng)或者小贈(zèng)品時(shí),就可以把活動(dòng)或者禮品圖放在靠前的輪播圖上。
- 首頁(yè)重點(diǎn)場(chǎng)景突出。如重點(diǎn)產(chǎn)品、常用功能入口,熱門活動(dòng)等場(chǎng)景。
同時(shí),市場(chǎng)上的輪播圖組件功能也越來(lái)越成熟,以下我們就介紹目前常見(jiàn)的實(shí)現(xiàn)輪播圖的方式。實(shí)現(xiàn)效果如下:
vant示例:

自實(shí)現(xiàn):

2.vue實(shí)現(xiàn)輪播圖
2.1 Vant組件引入
vant組件適用于移動(dòng)端項(xiàng)目,目前項(xiàng)目開源,是市面上做的比較好的開源項(xiàng)目,功能比較強(qiáng)大,我們先介紹vant實(shí)現(xiàn)輪播圖效果。對(duì)于pc端實(shí)現(xiàn)輪播圖,市場(chǎng)上也有成熟的組件,如Swipe。
2.1.1 vant組件引入
Vant組件安裝引入以及配置在前面我們已經(jīng)做過(guò)介紹,請(qǐng)產(chǎn)找以往鏈接:
- vue2:vue2引入vant組件
- vue3:vue3引入vant組件
2.2.2 使用van-swipe組件
van-swipe組件和van-swipe-item組件配合使用,原始代碼如下:
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" @change="onChange">
<van-swipe-item>vant-swipe</van-swipe-item>
<van-swipe-item class="dif">2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>可實(shí)現(xiàn)如下動(dòng)畫效果:

若是想進(jìn)行圖片輪播,vant組件還提供了懶加載選項(xiàng),以解決圖片加載過(guò)慢卡頓問(wèn)題,代碼如下:
<van-swipe :autoplay="3000" lazy-render>
<van-swipe-item v-for="image in images" :key="image">
<img :src="image" />
</van-swipe-item>
</van-swipe>其次,除了以上功能以外,還可以進(jìn)行自定義滑塊大小和改變輪播方向等操作,這里不做一一介紹,詳情請(qǐng)見(jiàn)官網(wǎng):
2.2 vue代碼實(shí)現(xiàn)
“魚”和”漁“相差還是比較大的,除了借助市場(chǎng)組件以外,還可以自己按照需求開發(fā)輪播圖,以下我們?cè)敿?xì)介紹一下自己實(shí)現(xiàn)輪播圖的思路與實(shí)現(xiàn)步驟,最后附上我們實(shí)現(xiàn)的代碼以供參考。
2.2.1 功能需求
- 實(shí)現(xiàn)圖片輪播;
- 輪播圖切換流暢;
- 可自動(dòng)輪播和手動(dòng)觸發(fā)。
2.2.2 實(shí)現(xiàn)思路
- 將需要輪播的圖片進(jìn)行排列;
- 添加手動(dòng)切換按鈕。
- 圖片切換之間添加動(dòng)畫效果;
- 添加切換邏輯,將排列好的圖片進(jìn)行切換;
2.2.3 代碼實(shí)現(xiàn)
實(shí)現(xiàn)思路代碼以及相關(guān)注釋如下:
<template>
<div class="carousel">
<div @click="prevSlide" class="carousel-prev-icon-left"></div>
<div class="carousel-slides">
<img
v-for="(image, index) in images"
:key="index"
:src="image"
:style="{left: index * 100 + '%', 'transform': dynamicstyle}"
alt="暫無(wú)圖片"
/>
</div>
<div @click="nextSlide" class="carousel-prev-icon-right"></div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
'/src/assets/img/sakuraTree01.jpeg',
'/src/assets/img/starrySky.jpg',
'/src/assets/img/starrySky02.jpg'
// ... 更多圖片
],
dynamicstyle: "", //動(dòng)態(tài)樣式
currentSlide: 0, //播放序號(hào)
interval: Object,
}
},
mounted() {
// 自動(dòng)播放動(dòng)畫
this.startSlideshow()
},
methods: {
nextSlide() {
// 每次指針加一
this.currentSlide = (this.currentSlide + 1) % this.images.length
this.setStyle();
},
prevSlide() {
// 每次減一,為負(fù)數(shù)時(shí)循環(huán)
this.currentSlide = (this.currentSlide - 1 + this.images.length) % this.images.length;
this.setStyle();
},
// 圖片動(dòng)畫
setStyle() {
this.dynamicstyle = `translatex(-${this.currentSlide*100}%)`
},
// 定時(shí)器
startSlideshow() {
this.interval = setInterval(() => {
this.currentSlide = (this.currentSlide + 1) % this.images.length;
this.setStyle();
}, 3000)
},
stopSlideshow() {
clearInterval(this.interval)
}
}
}
</script>
<style scoped>
.carousel {
position: relative;
}
.carousel-slides {
position: relative;
width: 320px;
height: 173px;
overflow: hidden;
}
.carousel-slides img {
display: inline-block;
position: absolute;
width: inherit;
margin: 0;
padding: 0;
top: 0;
left: 0;
height: 100%;
transition: 0.5s transform ease-in-out;
}
.carousel-controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content: center;
}
.carousel-prev-icon-left {
position: absolute;
left: 10px;
top: 55px;
height: 50px;
width: 40px;
border: none;
background-image: url(../../../assets/img/arrow-l.png);
background-size: contain;
background-repeat: no-repeat;
background-position: center;
z-index: 999;
}
.carousel-prev-icon-right {
position: absolute;
right: 10px;
top: 55px;
height: 50px;
width: 40px;
border: none;
background-image: url('../../../assets/img/arrow-r.png/');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
z-index: 999;
}
</style>以上代碼樣式部分還可以進(jìn)行精簡(jiǎn),僅供學(xué)習(xí)參考,商業(yè)項(xiàng)目需要進(jìn)行調(diào)試修改,思路可供參考。
需要注意以下幾點(diǎn):
- 圖片需要使用絕對(duì)定位;
- 動(dòng)態(tài)樣式的使用比較靈活;
- 使用動(dòng)畫平移要注意平移單位;
2.2.4 實(shí)現(xiàn)效果
實(shí)現(xiàn)效果動(dòng)圖如下:

3.總結(jié)
實(shí)現(xiàn)輪播圖的方式方法比較多,但用戶體驗(yàn)比較好的不多,查了市面上實(shí)現(xiàn)較好的就是使用動(dòng)畫進(jìn)行位置切換,有限推薦使用市面上比較成熟的組件,如果商業(yè)化的話,建議還是自己寫,避免版權(quán)糾紛。
以上就是vue利用vant組件實(shí)現(xiàn)輪播圖效果的詳細(xì)內(nèi)容,更多關(guān)于vue vant輪播圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作
這篇文章主要介紹了vue項(xiàng)目接口域名動(dòng)態(tài)獲取操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)過(guò)程詳解
前端的業(yè)務(wù)開發(fā)中會(huì)遇到不使用分頁(yè)方式來(lái)加載長(zhǎng)列表的需求,下面這篇文章主要給大家介紹了關(guān)于vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue中使用v-if,v-else來(lái)設(shè)置css樣式的步驟
我們?cè)谑褂胿ue項(xiàng)目開發(fā)時(shí),v-if是使用的非常多的,在這里我們談?wù)勅绾问褂胿-i來(lái)綁定修改css樣式,使用的主要是雙向數(shù)據(jù)綁定,即通過(guò)改變他的狀態(tài)來(lái)改變他的樣式,這篇文章主要介紹了vue中如何使用v-if,v-else來(lái)設(shè)置css樣式,需要的朋友可以參考下2023-03-03
基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
vue-cli腳手架的.babelrc文件用法說(shuō)明
這篇文章主要介紹了vue-cli腳手架的.babelrc文件用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
通過(guò)fastclick源碼分析徹底解決tap“點(diǎn)透”
這篇文章主要介紹了通過(guò)fastclick源碼分析徹底解決tap“點(diǎn)透”問(wèn)題的知識(shí)內(nèi)容,有興趣的朋友學(xué)習(xí)一下吧。2017-12-12

