vue實(shí)現(xiàn)輪播圖幀率播放
本文實(shí)例為大家分享了vue實(shí)現(xiàn)輪播圖幀率播放的具體代碼,供大家參考,具體內(nèi)容如下
需求
傳入一個數(shù)組,數(shù)組中放的是目錄名稱,通過本目錄名稱,讀取文件目錄下的所有圖片,并循環(huán)播放,形成一個每1s播放多少張圖片的效果,最后目錄播放完畢后,在跳到第一個目錄循環(huán)播放。
核心: 用 webpack的一個API require.contex讀取目錄下的文件名,具體想了解的可以查一查。
代碼
HTML
<template> <div id="imgPlay" ref="container" :style="[style]"> <img :src="imgsrc" :style="[{height:style.height,width:style.width}]"> <div id="but"> <button @click="start()">開始</button> <button @click="stop()">停止</button> </div> </div> </template>
javascript
<script> export default { name: 'ZxImgPlay', data () { return { style:[ width:"50px", height:"50px" ], interval: null, // 定時(shí)器id flag: true, // 定時(shí)器的開關(guān) setIntervalNumber: 0, // 當(dāng)前展示的圖片下標(biāo) imgsrc: "", // 要展示的圖片路徑 imgUrls: [], // 所有的圖片路徑 frameRate: 0 // 幀率 } }, computed: {}, watch: {}, created () { }, mounted () { this.zxInit() }, beforeDestroy () { }, methods: { zxInit () { // 這里的 this.DisplayParam 是公司內(nèi)部的一套東西,混入的對象 // this.DisplayParam.frameRate 是一個數(shù)組["目錄名1","目錄名2"] // this.DisplayParam.imgUrls 是死圖當(dāng)沒有目錄的時(shí)候就用死圖 // this.DisplayParam.frameRate 是傳入的幀率 this.frameRate = this.DisplayParam.frameRate && (1000 / this.DisplayParam.frameRate) this.imgUrls = this.DisplayParam.imgUrls this.DisplayParam.imageFileName != 0 ? this.readdir(this.DisplayParam.imageFileName) : this.showImages(true) }, start () { if (this.flag) return this.showImages() this.flag = true }, stop () { this.flag = false clearInterval(this.interval) }, readImages (imageFileName, _A) { this.stop() let req = require.context("@/../static/images", true, /\.(?:bmp|jpg|gif|jpeg|png)$/).keys(); let path = new RegExp(imageFileName[_A]) req.forEach(item => { if (path.test(item)) { this.imgUrls.push({ img: "@/../static/images/" + imageFileName[_A] + item.substring(item.lastIndexOf('/')) }) } }) this.showImages() }, readdir (imageFileName) { this.imgUrls = [] for (let i = 0; i < imageFileName.length; i++) { this.readImages(imageFileName, i) } }, showImages (_B) { if (_B) this.imgUrls = this.imgUrlsSort(this.imgUrls, 'sort') console.log(this.imgUrls) this.interval = setInterval(this.setIntervalFun, this.frameRate) }, imgUrlsSort (ary, key) { return ary.sort((a, b) => { let x = a[key]; let y = b[key]; return ((x < y) ? -1 : (x > y) ? 1 : 0) }) }, setIntervalFun () { if (this.setIntervalNumber >= this.imgUrls.length) { this.setIntervalNumber = 0 } this.imgsrc = this.imgUrls[this.setIntervalNumber++].img || '' } } } </script>
問題
上述這么做已經(jīng)實(shí)現(xiàn)了功能,但是目前來說是發(fā)現(xiàn)了兩個問題
1、require.context() 這個API它的第一個參數(shù)不能用一個可變的值,比如變量,會有警告。
2、上述代碼一直更換圖片的src實(shí)現(xiàn)的,也就是說每次換src時(shí)都會發(fā)送http請求獲取圖片,導(dǎo)致了內(nèi)存不會被釋放一直增加。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中import from的來源及省略后綴與加載文件夾問題
這篇文章主要介紹了Vue中import from的來源--省略后綴與加載文件夾,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Vue?echarts實(shí)例項(xiàng)目商家銷量統(tǒng)計(jì)圖實(shí)現(xiàn)詳解
Echarts,它是一個與框架無關(guān)的?JS?圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計(jì)IONIC也能用,因?yàn)槲业牧?xí)慣,每次新嘗試做一個功能的時(shí)候,總要新創(chuàng)建個小項(xiàng)目,做做Demo2022-09-09vue-element-admin如何設(shè)置默認(rèn)語言
這篇文章主要介紹了vue-element-admin如何設(shè)置默認(rèn)語言,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04Vue實(shí)現(xiàn)table列表項(xiàng)上下移動的示例代碼
本文主要介紹了Vue實(shí)現(xiàn)table列表項(xiàng)上下移動的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04如何解決Element UI el-dialog打開一次后無法再次打開問題
這篇文章主要介紹了如何解決Element UI el-dialog打開一次后無法再次打開問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02