vue实现轮播图帧率播放
作者:zpf吗 时间:2024-04-28 09:24:38
本文实例为大家分享了vue实现轮播图帧率播放的具体代码,供大家参考,具体内容如下
需求
传入一个数组,数组中放的是目录名称,通过本目录名称,读取文件目录下的所有图片,并循环播放,形成一个每1s播放多少张图片的效果,最后目录播放完毕后,在跳到第一个目录循环播放。
核心: 用 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, // 定时器id
flag: true, // 定时器的开关
setIntervalNumber: 0, // 当前展示的图片下标
imgsrc: "", // 要展示的图片路径
imgUrls: [], // 所有的图片路径
frameRate: 0 // 帧率
}
},
computed: {},
watch: {},
created () { },
mounted () {
this.zxInit()
},
beforeDestroy () { },
methods: {
zxInit () {
// 这里的 this.DisplayParam 是公司内部的一套东西,混入的对象
// this.DisplayParam.frameRate 是一个数组["目录名1","目录名2"]
// this.DisplayParam.imgUrls 是死图当没有目录的时候就用死图
// 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>
问题
上述这么做已经实现了功能,但是目前来说是发现了两个问题
1、require.context() 这个API它的第一个参数不能用一个可变的值,比如变量,会有警告。
2、上述代码一直更换图片的src实现的,也就是说每次换src时都会发送http请求获取图片,导致了内存不会被释放一直增加。
来源:https://blog.csdn.net/qq_44393247/article/details/113147062
标签:vue,轮播图,播放
0
投稿
猜你喜欢
IE和Firefox的js兼容性整理
2007-11-21 19:40:00
FCKeditor编辑器基本配置优化修改使用方法
2008-12-31 13:32:00
apache集成php7.3.5的详细步骤
2023-08-20 16:31:05
完美解决vue中报错 “TypeError: Cannot read properties of null (reading'forEach')“
2023-07-02 17:06:29
Go本地测试小技巧解耦任务拆解
2023-08-29 14:09:26
详解使用Selenium爬取豆瓣电影前100的爱情片相关信息
2021-12-04 10:52:23
Python中搜索和替换文件中的文本的实现(四种)
2022-04-23 01:03:39
element跨分页操作选择详解
2023-07-02 16:38:47
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
2023-06-19 20:06:16
pycharm: 恢复(reset) 误删文件的方法
2023-09-09 10:11:24
python验证码图片处理(二值化)
2023-09-14 15:29:02
MySQL基础之MySQL 5.7 新增配置
2024-01-15 08:19:02
从mysql到oracle你必须了解的50件事儿
2010-08-05 14:36:00
Mysql实战练习之简单图书管理系统
2024-01-17 14:09:32
python 爬取壁纸网站的示例
2022-07-10 13:29:54
Golang解析yaml文件操作指南
2024-05-09 14:51:59
python实现最速下降法
2023-08-10 18:19:51
Python实现日志实时监测的示例详解
2023-01-06 15:48:29
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
2022-09-09 18:33:23
SqlServer 获取字符串中小写字母的sql语句
2024-01-21 03:34:53