duanlu5055 2012-02-10 05:00
浏览 16
已采纳

图像替换

In the gallery of products I have choice to choose a color of item, seria, or side view. Each option has own picture. When I click one of these options I have src-substitution of image, for the effect I'm using fadeIn/fadeOut, it looks like:

$('button').click(function(){
 $('img').fadeOut("slow",function(){
 $(this).attr("src",newSRC);
 $(this).fadeIn("slow");
});
});

but when fadeIn completed The picture does not have time to draw, even if it has already been loaded into the cache and it's looking very wierd for the site-gallery intercoms

I can not use preCache all images, because if the products will be a count of over 100 items the site will loading whole day, in the main case at low connections. I wanted to remove item fully, and then use load, but I can't remove items 'caz the gallery will crash (it's a flexible site, I can't remove items, all will collapse). Now I did a little gif, but ... facepalm, sorry.

So what do you think the best solution could be ?

  • 写回答

2条回答 默认 最新

  • douying9296 2012-02-10 05:31
    关注

    I'd start the loading of the new image right away (into a temporary image object) on the click so it's available sooner (perhaps even before the fadeOut is done) rather than waiting until you actually need it to start the loading. This will get the image into the browser cache so it will load immediately when you assign the src of the real image and there will be less waiting:

    $('button').click(function(){
        var imgLoaded = false, fadeDone = false;
        var fadeTarget = $('img');
    
        // fadeIn the new image when everything is ready
        function fadeIfReady() {
            if (imgLoaded && fadeDone) {
                fadeTarget.attr("src", newSrc).fadeIn("slow");
            }
        }
    
        // create temporary image for starting preload of new image immediately
        var tempImg = new Image();
        tempImg.onload = function() {
            imgLoaded = true;
            fadeIfReady();
        };
        tempImg.src = newSrc;
    
        // start the fadeOut and do the fadeIn when the fadeOut is done or 
        // when the image gets loaded (whichever occurs last)
        fadeTarget.fadeOut("slow",function(){
            fadeDone = true;
            fadeIfReady();
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM