简体   繁体   English

Windows Phone 8上未触发图像加载事件

[英]Image onload event not firing on Windows Phone 8

I wrote a image gallery jQuery plugin for a mobile site I'm developing. 我为正在开发的移动网站编写了一个图片库jQuery插件。 For each image in the gallery, I have low res and high res. 对于画廊中的每个图像,我都有低分辨率和高分辨率。

The gallery is displayed full screen, with each image populated initially with the low res images. 画廊全屏显示,每个图像最初都填充有低分辨率图像。 When a user swipes left/right to a given image, I want to began loading the high res image quietly and swap it with the low res when complete. 当用户向左/向右滑动到给定的图像时,我要开始安静地加载高分辨率图像,并在完成时将其与低分辨率交换。

Right now this is working on iOS and Android, but on Windows Phone 8 the onload event for the high res image doesn't appear to be firing, so the swap never takes place. 目前,这在iOS和Android上都可以使用,但是在Windows Phone 8上,高分辨率图像的onload事件似乎没有触发,因此交换从未发生。

Code: 码:

var image = images[images_i];
if (image.$el && image.full && image.$el.attr('src') != image.full) {
    var fullImage = new Image();
    fullImage.onload = function () {
        image.$el.attr('src', image.full);
    };
    fullImage.src = image.full;
}

(gallery generates an 'images' hash that contains each image url, jQuery object, metadata, etc) (图库会生成一个“图像”哈希,其中包含每个图像网址,jQuery对象,元数据等)

I also originally tried actually inserting a new hidden image into the DOM, and using jQuery to bind to the load event. 我最初还尝试将新的隐藏图像实际插入DOM中,并使用jQuery绑定到load事件。 However, I found that this didn't consistently fire if the image was already cached, so I had to add another check for $image.prop('complete'). 但是,我发现,如果图像已经被缓存,这并不会一直触发,因此我不得不为$ image.prop('complete')添加另一个检查。 This produced the same result on WP8. 这在WP8上产生了相同的结果。

Apparently I was really close with my first attempt but made the mistake of getting the order of things wrong. 显然,我的第一次尝试确实很贴心,但是犯了使事情顺序出错的错误。 All of the answers I have seen on SO about this issue only included the onload event handling for images, but didn't account for the fact that if IE already has cached the image, this event will not fire. 关于这个问题,我在SO上看到的所有答案仅包括对图像的onload事件处理,但没有考虑到以下事实:如果IE已经缓存了图像,则不会触发该事件。

Full solution: 完整解决方案:

var fullImage = new Image();
fullImage.onload = function () {
    $img.attr('src', fullImageUrl);
};
fullImage.src = image.full;
if ($(fullImage).prop('complete') {
    $img.attr('src', fullImageUrl);
}

$(fullImage).prop('complete') will return true if the image is already cached. 如果图像已经被缓存,则$(fullImage).prop('complete')将返回true。

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

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