简体   繁体   English

如何使用jQuery异步获取远程图像?

[英]How to get remote image using jQuery asynchronously?

I want to show a remote image on my page. 我想在页面上显示一个远程图像。 I use Bootstrap 2.3.2 Carousel. 我使用Bootstrap 2.3.2传送带。 All the information comes from another web site's RSS feed. 所有信息都来自另一个网站的RSS feed。 I get data into a div like the following: 我将数据放入div中,如下所示:

...
<div id="newsItem-<?php echo $i;?>" class="item" data-src="<?php echo $feed[$i]->image; ?>" data-alt="<?php echo $feed[$i]->title; ?>">
</div>
...

The images takes too long to load. 图像加载时间太长。 Page is loaded about 15 seconds. 页面加载约15秒。 So I have decided to load images after the page loading finished. 因此,我决定在页面加载完成后加载图像。

There could be various dimensions of the pictures to be displayed. 要显示的图片可能有各种尺寸。 I want to show the largest existing one. 我想展示现有的最大的一个。

For each news item, all the images may have different but similar dimensions such as 1024x768, 620x350, 528x350, 527x350. 对于每个新闻项,所有图像可能具有不同但相似的尺寸,例如1024x768、620x350、528x350、527x350。

I have written a jQuery script to achieve this but something is wrong. 我已经编写了一个jQuery脚本来实现这一目标,但是出了点问题。

jQuery(function () {
    jQuery("div[id^='newsItem-']").each(function () {

        var r = jQuery(this).attr("data-src");
        var r620 = r.replace(".jpg", "-620x350.jpg");
        var r527 = r.replace(".jpg", "-527x350.jpg");
        var r1024 = r.replace(".jpg", "-1024x678.jpg");
        var r528 = r.replace(".jpg", "-528x350.jpg");
        var altImg = jQuery(this).attr("data-alt");

        if (pictureExists(r1024)){
            r = r1024;
        }
        else if (pictureExists(r620)){
            r = r620;
        }
        else if (pictureExists(r528)){
            r = r528;
        }
        else if (pictureExists(r527)){
            r = r527;
        }

        jQuery(this).prepend("<img src='" + r + "' alt='" + altImg + "' />");
        jQuery(this).removeAttr("data-alt");
        jQuery(this).removeAttr("data-src");
    });
});


function pictureExists(url) {
    var img = new Image();
    img.src = url;
    if (img.height !== 0) {
        return false;
    } else {
        return true;
    }
}

I want to display the largest existing picture in the carousel. 我想在轮播中显示最大的现有图片。

You cannot know the height/width of the image until its loaded. 在加载图像之前,您无法知道图像的高度/宽度。 So its an async process. 因此,它是一个异步过程。

In pictureExists function try to do it in this way: 在pictureExists函数中尝试以这种方式进行操作:

/ Create new image
var img = new Image();

// Create var for image source
var imageSrc = "http://example.com/blah.jpg";

// define what happens once the image is loaded.
img.onload = function() {
// Stuff to do after image load ( jQuery and all that )
// Within here you can make use of src=imageSrc, 
// knowing that it's been loaded.
};

// Attach the source last. 
// The onload function will now trigger once it's loaded.
img.src = imageSrc;

If you want to use the above way then you will have to implement promise structure to tackle the async nature of the image load to fetch the height/width 如果要使用上述方法,则必须实现promise结构,以解决图像加载的异步特性以获取高度/宽度

Or you can use this small plugin. 或者您可以使用这个小插件。 https://github.com/desandro/imagesloaded https://github.com/desandro/imagesloaded

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

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