vue實(shí)現(xiàn)輪播圖幀率播放
本文實(shí)例為大家分享了vue實(shí)現(xiàn)輪播圖幀率播放的具體代碼,供大家參考,具體內(nèi)容如下
需求
傳入一個(gè)數(shù)組,數(shù)組中放的是目錄名稱,通過(guò)本目錄名稱,讀取文件目錄下的所有圖片,并循環(huán)播放,形成一個(gè)每1s播放多少?gòu)垐D片的效果,最后目錄播放完畢后,在跳到第一個(gè)目錄循環(huán)播放。
核心: 用 webpack的一個(gè)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()">開(kāi)始</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í)器的開(kāi)關(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)部的一套東西,混入的對(duì)象
// this.DisplayParam.frameRate 是一個(gè)數(shù)組["目錄名1","目錄名2"]
// this.DisplayParam.imgUrls 是死圖當(dāng)沒(méi)有目錄的時(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>
問(wèn)題
上述這么做已經(jīng)實(shí)現(xiàn)了功能,但是目前來(lái)說(shuō)是發(fā)現(xiàn)了兩個(gè)問(wèn)題
1、require.context() 這個(gè)API它的第一個(gè)參數(shù)不能用一個(gè)可變的值,比如變量,會(huì)有警告。
2、上述代碼一直更換圖片的src實(shí)現(xiàn)的,也就是說(shuō)每次換src時(shí)都會(huì)發(fā)送http請(qǐng)求獲取圖片,導(dǎo)致了內(nèi)存不會(huì)被釋放一直增加。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用vue-json-viewer展示JSON數(shù)據(jù)的詳細(xì)步驟
最近在開(kāi)發(fā)一個(gè)公司的投放管理系統(tǒng)的操作日志模塊,要查看某條操作日志的請(qǐng)求參數(shù),要將請(qǐng)求的參數(shù)以JSON格式的形式展示出來(lái),下面這篇文章主要給大家介紹了vue使用vue-json-viewer展示JSON數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-09-09
vue+vue-validator 表單驗(yàn)證功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+vue-validator 表單驗(yàn)證功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
vue3+element?Plus實(shí)現(xiàn)表格前端分頁(yè)完整示例
這篇文章主要給大家介紹了關(guān)于vue3+element?Plus實(shí)現(xiàn)表格前端分頁(yè)的相關(guān)資料,雖然很多時(shí)候后端會(huì)把分頁(yè),搜索,排序都做好,但是有些返回?cái)?shù)據(jù)并不多的頁(yè)面,或者其他原因不能后端分頁(yè)的通常會(huì)前端處理,需要的朋友可以參考下2023-08-08
vue 中 element-ui table合并上下兩行相同數(shù)據(jù)單元格
這篇文章主要介紹了vue 中 element-ui table合并上下兩行相同數(shù)據(jù)單元格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無(wú)效的解決
這篇文章主要介紹了Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
淺談vue后臺(tái)管理系統(tǒng)權(quán)限控制思考與實(shí)踐
這篇文章主要介紹了淺談vue后臺(tái)管理系統(tǒng)權(quán)限控制思考與實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
vue項(xiàng)目中實(shí)現(xiàn)圖片預(yù)覽的公用組件功能
小編接到查看影像的功能需求,根據(jù)需求,多個(gè)組件需要用到查看影像的功能,所以考慮做一個(gè)公用組件,通過(guò)組件傳值的方法將查看影像文件的入?yún)鬟^(guò)去。下面小編通過(guò)實(shí)例代碼給大家分享vue項(xiàng)目中實(shí)現(xiàn)圖片預(yù)覽的公用組件功能,需要的朋友參考下吧2018-10-10
vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容
這篇文章主要介紹了vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

