简体   繁体   English

为什么这个图像预加载不起作用?

[英]How come this image preload isn't working?

I have this function in a file called plugins.js :我在一个名为plugins.js的文件中有这个 function :

$.fn.preload = function(){
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

Then I have this in another file called footer.js :然后我在另一个名为footer.js的文件中有这个:

$(['img1.jpg','img2.jpg','img3.jpg']).preload();

Shouldn't all this preload the images?所有这些不应该预加载图像吗?

The images are all in a folder called "graphics".这些图像都在一个名为“图形”的文件夹中。

I suspect the images are not preloaded because it takes a little while for the images to load when I am hovering the mouse over a button (I use the images for mouseOver effects).我怀疑图像没有预加载,因为当我将鼠标悬停在按钮上时加载图像需要一点时间(我将图像用于 mouseOver 效果)。

Is there any method to check for sure that the images have been preloaded?有什么方法可以检查图像是否已预加载? Can I do so with the above script?我可以用上面的脚本这样做吗?

I am not very good with jquery so code help is appreciated.我对 jquery 不是很好,因此非常感谢代码帮助。

Thanks谢谢

Works fine for me:对我来说很好:

Example: http://jsfiddle.net/98rXx/示例: http://jsfiddle.net/98rXx/

<button id='click'>click here</button>
$.fn.preload = function(){
    this.each(function(){
        $('<img/>').load(function(){
            alert('loaded');
        })[0].src = this;
    });
};

$(["http://dummyimage.com/120x90/f00/fff.png&text=my+image"]).preload();

  // clicking the button displays the image immediately
$('button').click(function() {
    $('<img>',{src:"http://dummyimage.com/120x90/f00/fff.png&text=my+image"})
        .appendTo(document.body);
});

Though I think you'd be better off making it more of a utility:虽然我认为你最好让它更像一个实用程序:

$.preload = function( arr ){
    $.each( arr, function(i,v){
        $('<img/>')[0].src = v;
    });
};

$.preload(['img1.jpg','img2.jpg','img3.jpg']);

There is no method which will say whether the images have been preloaded or not.没有方法可以说明图像是否已预加载。 You can use image load event to determine whether image is loaded or not.您可以使用图像load事件来确定是否加载了图像。

Eg例如

$("img").load(function(){
   //Image loaded
});

There is also error event which gets triggered if the image load fails due to any reason如果由于任何原因图像加载失败,也会触发错误事件

 $("img").error(function(){
       //Image load failed
 });

Good day sir, you can check wether your images are preloaded by using firebug in firefox or the developer console in chrome.早上好,先生,您可以使用 firefox 中的 firebug 或 chrome 中的开发人员控制台检查您的图像是否已预加载。 They both have tabs (Net adn Network respectively) that let you monitor all the requests your browser makes.它们都有标签(分别是 Net 和 Network),可让您监控浏览器发出的所有请求。

Firefox also has the developer tools plugin that will let you turn off caching. Firefox 还具有开发人员工具插件,可让您关闭缓存。 This combined with the Net tab should give you an accurate picture of what is loaded on the webpage before doing anything at all on the webpage itself after refreshing.这与“网络”选项卡相结合,可以让您准确了解网页上加载的内容,然后再在刷新后对网页本身进行任何操作。

As for the preload script;至于预加载脚本; you might want to check this out:你可能想看看这个:

http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript

Your script as it is should work, but if the images are in a different directory(graphics) you need to extend the path:您的脚本应该可以工作,但如果图像位于不同的目录(图形)中,您需要扩展路径:

$.fn.preload = function(){
    this.each(function(){
        $('<img/>')[0].src = 'graphics/'+this;
    });
}

The way I do it is using the Image object:我这样做的方式是使用图像 object:

var img = new Image();
img.src = 'image.jpg';
img.onload = function(){
    alert('Image has been preloaded! width: '+ this.width + ', height: '+this.height);
};
img.onload = function(){
    alert('There was an error loading: '+this.src);
}

So your plugin should look something like this (untested):所以你的插件应该看起来像这样(未经测试):

$.fn.preload = function(){
    var img = new Image();
    this.each(function(){
        img.src = this;
    });
}

Still, you need to use the onload callback to make sure all images where loaded, or just wait.尽管如此,您仍需要使用 onload 回调来确保所有图像都已加载,或者只是等待。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM