jquery - 图像预加载 jquery

标签 jquery html css

我正在寻找使用 jQuery 预加载图像的最佳方式。

我试过使用这段代码,但它似乎不起作用:

    function preload(arrayOfImages) {
        $(arrayOfImages).each(function(){
            $('<img/>')[0].src = this;
            // Alternatively you could use:
            // (new Image()).src = this;
        });
    }

    // Usage:

    preload([
        'img/preload.jpg'
    ]);

这是我的标记:

                <div class="item">
                    <img src="img/lookbook_item1.jpg" alt="lookbook_item1" width="187" height="259">
                </div>

                <div class="item">
                    <img src="img/lookbook_item1.jpg" alt="lookbook_item1" width="187" height="259">
                </div>

                <div class="item">
                    <img src="img/lookbook_item2.jpg" alt="lookbook_item2" width="383" height="259">
                </div>

我不想使用插件,只是一个可以执行淡入或类似操作的小脚本。

最佳答案

这样试试:

$.each( arrayOfImages, function( i, src ) {
  var img = new Image()
  img.src = src;
  img.load = function() {
    $("img[src=" + src + "]").fadeIn();
  };
});

关于jquery - 图像预加载 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376138/

相关文章:

javascript - Knockout Kendo 下拉列表获取所选项目的文本

php - 我的 jQuery 变量在读取时为空

javascript - 将标题文本作为输入值 jQuery

javascript - 在页面上追加后元素未定义

android - ionic 标签恼人的激活颜色

html - OOCSS & BEM - 内联列表 - 如何处理脚手架,如何添加皮肤?

javascript - CSS Layout 液柱问题包装内容

html - 换行符 <hr> 未按预期呈现

html - 使用 phonegap 2.6 和 iOS 处理外部链接

css - 你应该同时使用 bootstrap.css + normalize.css 吗?