Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k 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 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...