Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

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";
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

使用雪碧图+background-position,使用img替代都有人提过了,都是可行的。

如果你的场景需要使用background-image来切换,可以把要切换的图片先用img标签放入页面中,父级使用display:none; 隐藏掉这些图片。
然后background-image切换时就不闪烁了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...