循环播放:
用定时器,图片路径后面加随机数
图片闪烁:
图片预加载
下面贴代码:
mounted(){
this.timer= setInterval(()=>{
this.bgGif= require('../../assets/main/bgGif.gif')+'?'+Math.random()//添加随机数
this.loadImage(this.bgGif,this.setImage)
},700)
},
loadImg(url,callback) {
var img = new Image();
img.src = url;
img.onload = function(){//图片预加载
callback(img);
};
},
setImg(img){
this.bgGif="url("+img.src+")"+"no-repeat center -50px/ 92% #152348"
},
// 关闭定时器
destroyed(){
clearInterval(this.timer)
}
这篇博客介绍了如何在Vue应用中利用定时器和图片预加载技术实现图片的循环播放和闪烁效果。通过在图片URL后添加随机数避免浏览器缓存,确保每次加载不同的图片。同时,详细讲解了`mounted`、`loadImg`和`setImg`等关键函数的实现,确保图片的顺利切换。在组件销毁时,还正确地清理了定时器,防止内存泄漏。
1997

被折叠的 条评论
为什么被折叠?



