检测图像 jQuery插件已加载 imagesLoaded

  • 源码大小:156.39KB
  • 所需积分:1积分
  • 源码编号:19JP-3496
  • 浏览次数:1072次
  • 最后更新:2023年06月20日
  • 所属栏目:加载
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

imagesLoaded是一个有用的jQuery&Vanilla JavaScript插件,用于检测和检查容器中的图像是否已加载,然后触发事件进行进一步操作。

跨平台和移动友好。支持两者国际货币基金组织标签和背景图像。

相关插件:

  • 用于检查内容的jQuery插件已加载-正在加载
  • 使用Javascript预加载图像的jQuery插件-图像加载器

如何使用它:

1.从CDN加载imagesLoaded库。

  1. <!-- jQuery is optional -->
  2. <script src="jquery.min.js"></script>
  3. <!-- Minified Version -->
  4. <script src="https://unpkg.com/[email protected]/imagesloaded.pkgd.min.js"></script>

2.检测是否所有图像都已加载到您指定的容器中。

  1. // jQuery
  2. $('.myContainer').imagesLoaded({
  3. // options here
  4. }, function() {
  5. // do something after loaded
  6. });
  7.  
  8. // Vanilla JavaScript
  9. imagesLoaded(document.querySelector('.myContainer'), {
  10. // options here
  11. }, function() {
  12. // do something after loaded
  13. });
  1. $('#container').imagesLoaded()
  2. .always( function( instance ) {
  3. console.log('all images loaded');
  4. })
  5. .done( function( instance ) {
  6. console.log('all images successfully loaded');
  7. })
  8. .fail( function() {
  9. console.log('all images loaded, at least one is broken');
  10. })
  11. .progress( function( instance, image ) {
  12. var result = image.isLoaded ? 'loaded' : 'broken';
  13. console.log( 'image is ' + result + ' for ' + image.img.src + );
  14. });

3.检测背景图像的加载。

  1. // jQuery
  2. $('.myContainer').imagesLoaded({
  3. background: true
  4. }, function() {
  5. // do something after loaded
  6. });
  7.  
  8. // Vanilla JavaScript
  9. imagesLoaded(document.querySelector('.myContainer'), {
  10. background: true
  11. }, function() {
  12. // do something after loaded
  13. });

4.事件处理程序

  1. // jQuery
  2. $('.myContainer').imagesLoaded()
  3. .always( function( instance ) {
  4. // fired after all images are loaded
  5. })
  6. .done( function( instance ) {
  7. // fired after all images are successfully loaded
  8. })
  9. .fail( function() {
  10. // fired after all images are loaded, at least one is broken
  11. })
  12. .progress( function( instance, image ) {
  13. var result = image.isLoaded ? 'loaded' : 'broken';
  14. console.log( 'image is ' + result + ' for ' + image.img.src );
  15. });
  16.  
  17. // Vanilla JavaScript
  18. imagesLoaded(document.querySelector('.myContainer'))
  19. on('always', function( instance ) {
  20. // fired after all images are loaded
  21. })
  22. on('done', function( instance ) {
  23. // fired after all images are successfully loaded
  24. })
  25. on('fail', function( instance ) {
  26. // fired after all images are loaded, at least one is broken
  27. })
  28. on('progress', function( instance, image ) {
  29. var result = image.isLoaded ? 'loaded' : 'broken';
  30. console.log( 'image is ' + result + ' for ' + image.img.src );
  31. });

更新日志:

v5.0.0版本(2022-07-04)

  • ES6,删除IE

版本4.1.4(2019年7月5日)

  • 版本更新
  • 文档更新

第4.1.1版(2016年8月28日)

  • 版本更新

预览截图