vue实现轮播图帧率播放
这篇文章主要为大家详细介绍了vue实现轮播图帧率播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了vue实现轮播图帧率播放的具体代码,供大家参考,具体内容如下
需求
传入一个数组,数组中放的是目录名称,通过本目录名称,读取文件目录下的所有图片,并循环播放,形成一个每1s播放多少张图片的效果,最后目录播放完毕后,在跳到第一个目录循环播放。
核心: 用 webpack的一个API require.contex读取目录下的文件名,具体想了解的可以查一查。
代码
HTML
1 2 3 4 5 6 7 8 9 | < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <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请求获取图片,导致了内存不会被释放一直增加。
以上就是本文的全部内容,希望对大家的学习有所帮助