jquery – 检查图像是否加载?

前端之家收集整理的这篇文章主要介绍了jquery – 检查图像是否加载?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一个sulotion,检查所有图像是否在图像滑块/旋转器中使用之前加载。我正在考虑一个解决方案,当主图像被加载时,只显示图像的按钮或缩略图,以防止用户点击已经下载完整的图像。

解决方法

来自 .ready jQuery文档的文本可能有助于区分加载和准备更清楚:

While JavaScript provides the load event for executing code when a page is rendered,this event does not get triggered until all assets such as images have been completely received. In most cases,the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready,so this is usually the best place to attach all other event handlers and run other jQuery code.

…和.load文档:

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images,scripts,frames,iframes,and the window object.

所以.load是你要找的。这个事件的好处是可以将其附加到DOM的一个子集。假设你的幻灯片图像是像这样的div:

<div class="slideshow" style="display:none">
  <img src="image1.png"/>
  <img src="image2.png"/>
  <img src="image3.png"/>
  <img src="image4.png"/>
  <img src="image5.png"/>
</div>

…然后,只要在.slideshow容器中,只要载入容器中的所有图像,就可以使用.load,而不管页面上的其他图像如何。

$('.slideshow img').load(function(){
  $('.slideshow').show().slideshowPlugin();
});

(我放在一个显示器:没有一个例子,它会在加载时隐藏图像。)

更新(03.04.2013)自jQuery版本1.8以来,此方法已被弃用

原文链接:https://www.f2er.com/jquery/181979.html

猜你在找的jQuery相关文章