Home  >  Q&A  >  body text

js 改变background-image 图片都会闪一下怎么办?

用js改变backgroundImage属性,在iphone上测试,每次更换图片都会闪一下。

update:只有iphone会闪,使用了onload也闪,代码如下

  function selection(num0, num1, num2) {
            var img = new Image();
            img.src = urls[num0][1];
            img.onload = function () {
                console.log("complete");
                selections[num0].style.backgroundImage = "url(" + urls[num0][1] + ")";
                list[num0].style.display = "block";
                selections[num1].style.backgroundImage = "url(" + urls[num1][0] + ")";
                list[num1].style.display = "none";
                selections[num2].style.backgroundImage = "url(" + urls[num2][0] + ")";
                list[num2].style.display = "none";
            }
        }
城堡下的诡洞城堡下的诡洞2783 days ago1198

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-10-01 00:45:17

    js What should I do if the image flashes when I change the background-image? -PHP Chinese website Q&A-js What should I do if the image flashes when I change the background-image? -PHP Chinese website Q&A

    Let’s take a look and learn.

    reply
    0
  • 迷茫

    迷茫2017-03-09 09:24:46

    因为修改了图片后,新修改的图片没有下载过,所以是空白的

    你可以把要修改的图片创建img标签隐藏起来,做个预加载


    reply
    0
  • Cancelreply