function eight(){
divid = document.getElementById('eight');
i = 1;
function bimg(){
if(i<=8){
divid.style.backgroundImage = 'url("../images/eightimg/eight_'+i+'.gif")';
i++
}else{
i = 1;
}  
 }
 imgb = setInterval(bimg,5000);
}
这个代码动态改变id为eight的div的背景图片,为什么每变换一张图片页面就要闪一下,有什么方法可以不让他闪么?

解决方案 »

  1.   

    应该只是ie6才会闪动吧
    加这句试试
    document.all&&document.execCommand("BackgroundImageCache", false, true); 
      

  2.   

    问题解决了,加一个函数让图片预加载,就不闪了,
        var imgar = [];
    function eightimg(){
    for(i=1;i<9;i++){
      imgar[i] = new Image();
      imgar[i].src = "../images/eightimg/eight_"+i+".gif"; 
      imgar[i].onload=function(){
    this.width = 960;
    this.height =380;
       }
     }
    }
    function eight(){
    divid = document.getElementById('eight');
    i = 1;
    function bimg(){
    if(i<=8){
    divid.style.backgroundImage = 'url("'+imgar[i].src+'")';
    i++
    }else{
    i = 1;
    }  
     }
     imgb = setInterval(bimg,5000);
    }
    这样就不闪了,只用bimg函数每变换一次就要从服务器下载一次图片所以闪(我自己的理解不知道对不对),楼上的能讲讲document.all&&document.execCommand("BackgroundImageCache", false, true); 的作用么?
      

  3.   

    document.execCommand("BackgroundImageCache", false, true); 
    是将图片缓存在IE内存中, 前面的.all是判断是否为ieie6默认图片不入cache. 避免闪烁的方法就这样了问题解决了就好了
      

  4.   


    楼上的意思是 让你把所有的背景图片压缩到一张大图片上 然后通过position定位来切换.
    至于这个技术 楼主可以百度下 CSS Sprite