解决JS修改background-image时图片闪烁
stackoverflow上搜到的,记录一下
原文链接: How to prevent a background image flickering on change
解决方法:
// create a new Image object
var img_tag = new Image();
// when preload is complete, apply the image to the div
img_tag.onload = function() {
document.querySelector('#div').style.backgroundImage = 'url(' + img + ')';
}
// setting 'src' actually starts the preload
img_tag.src = img;