使用 JavaScript 异步加载图像

发布于 2024-07-27 19:38:55 字数 354 浏览 1 评论 0原文

有没有一种方法可以显示使用 Flickr 生成的动态图像的加载图像? 我遇到了一种方法,如 Wacom 社区 网站所示,但我无法获得让它工作。 有没有比 http://blog.realmofzod.com/asynchronous-image-loading-with-jquery/

Is there a method for showing a loading image for dynamic images that are generated using Flickr? I have come across a way to do it as shown on the Wacom Community site but I have not been able to get it to work. Is there something simpler or does anyone have a better explanation than the originator of the technique from http://blog.realmofzod.com/asynchronous-image-loading-with-jquery/?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不疑不惑不回忆 2024-08-03 19:38:55

我刚刚开始工作。 YMMV:

<img src="images/blank.gif" onload="replaceImage(this, 'flickrthumbnailimageurl')"
width="75" height="75" />

然后 replaceImage

function replaceImage(img, replacementImage)
{
    img.onload = null;
    img.src = replacementImage;
}

blank.gif 只是一个 1x1 像素的纯灰色图像。 基本上,这个想法是加载这个空白图像并将其扩展到 75x75(以保留布局)。 这几乎立即触发 onload 处理程序,将图像的源更改为所需的图像。 它有你想要的效果。

I just got this working. YMMV:

<img src="images/blank.gif" onload="replaceImage(this, 'flickrthumbnailimageurl')"
width="75" height="75" />

And then replaceImage:

function replaceImage(img, replacementImage)
{
    img.onload = null;
    img.src = replacementImage;
}

blank.gif is just a 1x1 pixel solid gray image. Basically, the idea is that this blank image is loaded and expanded to 75x75 (to preserve layout). That almost immediately fires the onload handler, which changes the image's source to the desired image. It has the effect you desire.

我们的影子 2024-08-03 19:38:55

可以用 jquery 做到:

<img src="http://myimages.com/loaderImage.jpg" id="imgIdLoading" />
<img src="http://flickr.com/image.jpg" id="imgId" style="display:none;" />

$('#imgId').load(function(){
    // ... loaded
    $('#imgIdLoading').remove();
    $('#imgId').show();  
}).error(function(){
    // ... not loaded
    $(this).attr('src','/whatever/error.png');
});

could do it with jquery:

<img src="http://myimages.com/loaderImage.jpg" id="imgIdLoading" />
<img src="http://flickr.com/image.jpg" id="imgId" style="display:none;" />

$('#imgId').load(function(){
    // ... loaded
    $('#imgIdLoading').remove();
    $('#imgId').show();  
}).error(function(){
    // ... not loaded
    $(this).attr('src','/whatever/error.png');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文