backTop

解决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;
posted @ 2022-02-24 16:38  ooops!  阅读(559)  评论(0编辑  收藏  举报