网络编程 
首页 > 网络编程 > 浏览文章

JavaScript控制图片加载完成后调用回调函数的方法

(编辑:jimmy 日期: 2025/12/28 浏览:3 次 )

本文实例讲述了JavaScript控制图片加载完成后调用回调函数的方法。分享给大家供大家参考。具体分析如下:

这段代码可以控制指定区域内的图片加载完成后执行指定的回调函数。
复制代码 代码如下:function when_images_loaded($img_container, callback) {
/* do callback when images in $img_container (jQuery object) are loaded. Only works when ALL images in $img_container are newly inserted images and this function is called immediately after images are inserted into the target. */
    var _imgs = $img_container.find('img'),
        img_length = _imgs.length,
        img_load_cntr = 0;
    if (img_length) {//if the $img_container contains new images.
        _imgs.on('load', function() {//then we avoid the callback until images are loaded
            img_load_cntr++;
            if (img_load_cntr == img_length) {
                callback();
            }
        });
    }
    else { //otherwise just do the main callback action if there's no images in $img_container.
        callback();
    }
}

希望本文所述对大家的javascript程序设计有所帮助。

上一篇:jQuery实现页面滚动时动态加载内容的方法
下一篇:jQuery UI插件自定义confirm确认框的方法
一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap