简体   繁体   English

jQuery检测何时加载一个图像-无插件

[英]jQuery Detect When One Image Has Loaded - Without Plugin

Using jQuery, is there a way to detect when one particular image is done loading? 使用jQuery,有没有一种方法可以检测何时加载一个特定图像? I've seen this plugin: http://desandro.github.io/imagesloaded/ but this should be a simple solution. 我看过这个插件: http : //desandro.github.io/imagesloaded/,但这应该是一个简单的解决方案。 I really don't want to download another plugin. 我真的不想下载另一个插件。

User clicks on this element: 用户单击此元素:

<input type="file" />

jQuery function places the image into this element: jQuery函数将图像放入以下元素:

<img src="" />

I want to determine when the image has finished loading. 我想确定图像何时完成加载。 Can this be done? 能做到吗? Thanks. 谢谢。

I made a jsfiddle with a really rough working example of .load() working here . 我做了一个用的jsfiddle的.load一个非常粗略的工作示例()工作在这里

HTML HTML

<figure class="button">button</figure>
<div id="imgload"></div>

CSS CSS

figure {
    width: 50px;
    height: 20px;
    line-height: 20px;
    background: #ccc;
    text-align: center;
}

#imgload {display:none;}

jQuery jQuery的

$('.button').on('click', function(){
    $('#imgload').html('<img src="http://placekitten.com/700/400"/>');

    $('#imgload img').load(function(){
        $('#imgload').fadeIn(400);
    });
});

On click, the image is placed in the div and then after it finishes loading, the div fades in. So it successfully detects when the image has finished loading. 单击时,将图像放置在div中,然后在完成加载后div淡入。因此,它可以成功检测图像何时完成加载。 I've used this technique a hundred times. 我已经使用这种技术一百次了。

EDIT: cleaned it up a little bit 编辑:清理了一点

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

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