Vue中使用better-scroll實(shí)現(xiàn)輪播圖組件
better-scroll 是什么
better-scroll 是一款重點(diǎn)解決移動(dòng)端(已支持 PC)各種滾動(dòng)場景需求的插件。它的核心是借鑒的 iscroll 的實(shí)現(xiàn),它的 API 設(shè)計(jì)基本兼容 iscroll,在 iscroll 的基礎(chǔ)上又?jǐn)U展了一些 feature 以及做了一些性能優(yōu)化。
better-scroll 是基于原生 JS 實(shí)現(xiàn)的,不依賴任何框架。它編譯后的代碼大小是 63kb,壓縮后是 35kb,gzip 后僅有 9kb,是一款非常輕量的 JS lib。
今天我們利用它實(shí)現(xiàn)一個(gè)橫向滾動(dòng)——輪播圖組件。演示如下:
首先來整理一下需求:
- 能夠根據(jù)異步請(qǐng)求到的圖片數(shù)據(jù)進(jìn)行輪播圖展示。
- 能夠控制它是否自動(dòng)播放,是否循環(huán)播放,自動(dòng)播放間隔。
- 能夠提示當(dāng)前播放頁。
Mock數(shù)據(jù)
由于是一個(gè)demo,從網(wǎng)上找了幾張圖片寫成json格式,數(shù)據(jù)用于模擬接口數(shù)據(jù)。這里用到了mock.js。Axios。安裝方法如下:
npm install mockjs
npm install --save axios vue-axios
axios使用方法不多贅述,簡述一下mock數(shù)據(jù)。在mock文件夾下新建json文件夾放置json數(shù)據(jù)文件。新建index.js導(dǎo)出接口。就可以使用axios請(qǐng)求接口了。
[ "https://img3.mukewang.com/szimg/5df8852609e0762d12000676-360-202.png", "https://img1.mukewang.com/szimg/5d9c62fb0907ccf012000676-360-202.png", "https://img3.mukewang.com/5aeecb1d0001e5ea06000338-360-202.jpg" ]
const Mock = require('mockjs')
Mock.mock('/slider', 'get', require('./json/slider.json'))
基礎(chǔ)組件:slider.vue
將輪播圖組件抽象出來,接收isLoop、isAutoPlay、interval屬性控制輪播圖。從mounted方法調(diào)用順序可以知道思路是
- setSliderWidth()中先獲取再設(shè)置顯示層和圖片包裹層高度。
- initDots()根據(jù)圖片包裹層子元素的個(gè)數(shù)設(shè)置數(shù)組放置圓點(diǎn)。
- initSlider()初始化better-scroll。
- autoPlay()設(shè)置自動(dòng)播放。
<template>
<div class="slider-apply" ref="slider"> <!-- 顯示層 -->
<div class="slider-group" ref="group"> <!-- 所有圖片包裹層 -->
<slot></slot> <!-- 插槽顯示圖片內(nèi)容 -->
</div>
<div class="dots"> <!-- 提示圓點(diǎn) -->
<div class="dot" v-for="(item, index) in dots" :key="index" :class="currentIndex===index?'active':''"></div>
</div>
</div>
</template>
<script type='text/ecmascript-6'>
import BScroll from 'better-scroll'
export default {
data () {
return {
dots: [],
currentIndex: 0 /* 當(dāng)前頁下標(biāo) */
}
},
props: {
isLoop: { /* 循環(huán)播放 */
type: Boolean,
default: true
},
isAutoPlay: { /* 自動(dòng)播放 */
type: Boolean,
default: true
},
interval: { /* 播放間隔 */
type: Number,
default: 2000
}
},
mounted () { /* mounted階段dom渲染完,20ms確保刷新 */
setTimeout(() => {
this.setSliderWidth()
this.initDots()
this.initSlider()
if (this.isAutoPlay) {
this.autoPlay()
}
}, 20)
},
methods: {
setSliderWidth () { /* 獲取顯示層寬度,計(jì)算內(nèi)容層寬度 */
const clientWidth = this.$refs.slider.clientWidth
let sliderWidth = 0
this.children = this.$refs.group.children
for (let i = 0; i < this.children.length; i++) {
this.children[i].style.width = clientWidth + 'px'
sliderWidth += clientWidth
}
if (this.isLoop) { /* 循環(huán)播放需要增加前后兩個(gè)寬度 */
sliderWidth += clientWidth * 2
}
this.$refs.group.style.width = sliderWidth + 'px' /* 設(shè)置內(nèi)容層寬度 */
},
initDots () {
this.dots = new Array(this.children.length)
},
initSlider () {
this.slider = new BScroll(this.$refs.slider, {
scrollX: true, /* 橫向滾動(dòng) */
scrollY: false,
snap: { /* 循環(huán)滾動(dòng)設(shè)置 */
loop: this.isLoop,
threshold: 0.3,
speed: 400
}
})
this.slider.on('scrollEnd', () => {
const pageIndex = this.slider.getCurrentPage().pageX /* 獲取當(dāng)前輪播頁,用于圓點(diǎn)提示 */
this.currentIndex = pageIndex
if (this.isAutoPlay) {
clearTimeout(this.timer) /* 重新設(shè)置自動(dòng)播放,否則無法自動(dòng)播放 */
this.autoPlay()
}
})
},
autoPlay () {
this.timer = setTimeout(() => {
this.slider.next(400)
}, this.interval)
}
},
destroyed () { /* 確保清除定時(shí)器 */
clearTimeout(this.timer)
}
}
</script>
<style lang="stylus" scoped>
.slider-apply
position relative // 讓dots找準(zhǔn)位置
height 200px
width 100% // slider-apply會(huì)依據(jù)父元素寬度顯示寬度
overflow hidden // 超出元素隱藏
border-radius 5px
.dots
position absolute
bottom 10px
left 50%
transform translate(-50%, 0) // 居中
display flex
.dot
margin 0 10px
height 7px
width 7px
background #fff
border-radius 50%
.active // 當(dāng)前dot樣式
width 15px
border-radius 50% 5px
</style>
應(yīng)用組件:slider-apply.vue
可以根據(jù)alider-apply.vue中的使用方法應(yīng)用在自己的項(xiàng)目中。
<template>
<div class="slider-wrapper">
<Slider v-if="showSlider"> <!-- showSlider使得數(shù)據(jù)請(qǐng)求完成后再顯示,否則better-scroll可能會(huì)計(jì)算錯(cuò)誤 -->
<div v-for="item in imageList" :key="item" class="slider-item">
<img :src="item" class="img">
</div>
</Slider>
</div>
</template>
<script type='text/ecmascript-6'>
import Slider from 'base/slider'
export default {
data () {
return {
imageList: [], // 圖片列表
showSlider: false // 顯示slider標(biāo)志位
}
},
created () {
this.getImages() // 獲取數(shù)據(jù)
},
methods: {
getImages () {
this.axios.get('/slider').then((res) => {
this.imageList = res.data
this.showSlider = true
}).catch((err) => {
console.log(err)
})
}
},
components: {
Slider
}
}
</script>
<style lang="stylus" scoped>
.slider-wrapper
margin 0 auto
height 200px // 固定輪播圖顯示高度
width 500px // 固定輪播圖顯示寬度,可設(shè)置百分比
background #000
border-radius 5px
.slider-item
float left // 元素向左浮動(dòng)
width 100%
overflow hidden
text-align center
.img
height 200px
width 100%
</style>
如果以上步驟沒有看明白的話,可以在我的github中找到源碼https://github.com/Gesj-yean/vue-demo-collection。
總結(jié)
到此這篇關(guān)于Vue中使用better-scroll實(shí)現(xiàn)輪播圖組件的文章就介紹到這了,更多相關(guān)vue 輪播圖組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通用vue組件化展示列表數(shù)據(jù)實(shí)例詳解
組件化開發(fā)能大幅提高應(yīng)用的開發(fā)效率、測試性、復(fù)用性等,下面這篇文章主要給大家介紹了關(guān)于通用vue組件化展示列表數(shù)據(jù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
vue中數(shù)組常用的6種循環(huán)方法代碼示例
在vue項(xiàng)目開發(fā)中,我們需要對(duì)數(shù)組進(jìn)行處理等問題,這里簡單記錄遍歷數(shù)組的幾種方法,這篇文章主要給大家介紹了關(guān)于vue中數(shù)組常用的6種循環(huán)方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
一篇文章帶你吃透Vue生命周期(結(jié)合案例通俗易懂)
這篇文章主要給大家介紹了關(guān)于如何通過一篇文章帶你吃透Vue生命周期,文章通過結(jié)合案例更加的通俗易懂,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解
在一次項(xiàng)目腳手架升級(jí)的過程中,將之前基于?webpack?搭建的項(xiàng)目移植到?Vite?構(gòu)建,這篇文章主要介紹了在?Vite項(xiàng)目中,使用插件?@rollup/plugin-inject?注入全局?jQuery,需要的朋友可以參考下2022-12-12
Vue3中的極致防抖/節(jié)流詳解(附常見方式防抖/節(jié)流)
在JavaScript中函數(shù)的防抖和節(jié)流不是什么新鮮話題,這篇文章主要給大家介紹了關(guān)于Vue3中極致防抖/節(jié)流的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

