mui.lazyload.img.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (function($, window, document) {
  2. var ImageLazyload = $.Lazyload.extend({
  3. init: function(element, options) {
  4. this._super(element, options);
  5. },
  6. _init: function() {
  7. this.options.selector = '[data-lazyload]';
  8. this._super();
  9. },
  10. _set: function(element, uri) {
  11. if (element.tagName === 'IMG') {
  12. element.src = uri;
  13. } else {
  14. element.style.backgroundImage = "url(" + uri + ")";
  15. }
  16. },
  17. _hasPlaceholder: function(element) {
  18. if (element.offsetWidth) {
  19. if (element.tagName === 'IMG') {
  20. return !!element.src;
  21. } else {
  22. return !!element.style.backgroundImage;
  23. }
  24. }
  25. return false;
  26. },
  27. _addPlaceHolder: function(element) {
  28. var self = this;
  29. if (element.tagName === 'IMG') {
  30. self._counter++;
  31. element.onload = function() {
  32. self._counter--;
  33. self.addCallback(element, self.handle);
  34. this.onload = null;
  35. };
  36. self.onPlaceHolder(function(placeholder) {
  37. self._set(element, placeholder);
  38. });
  39. } else {
  40. element.style.backgroundImage = "url(" + self.options.placeholder + ")";
  41. }
  42. },
  43. addElement: function(element) {
  44. var self = this;
  45. var uri = element.getAttribute('data-lazyload');
  46. if (uri) {
  47. if (self._hasPlaceholder(element)) {
  48. self.addCallback(element, self.handle);
  49. } else {
  50. self.onPlaceHolder = self._createLoader(function(callback) {
  51. var img = new Image();
  52. var placeholder = self.options.placeholder;
  53. img.src = placeholder;
  54. img.onload = img.onerror = function() {
  55. callback(placeholder);
  56. };
  57. });
  58. self._addPlaceHolder(element);
  59. }
  60. return true;
  61. }
  62. return false;
  63. },
  64. set: function(element, uri) {
  65. var self = this;
  66. var img = new Image();
  67. img.onload = function() {
  68. self._set(element, uri);
  69. $.trigger(self.element, 'success', {
  70. element: element,
  71. uri: uri
  72. });
  73. };
  74. img.onerror = function() {
  75. $.trigger(self.element, 'error', {
  76. element: element,
  77. uri: uri
  78. });
  79. };
  80. img.src = uri;
  81. element.removeAttribute('data-lazyload'); //只尝试一次,后续可能支持多次尝试
  82. },
  83. handle: function(element, key) {
  84. var uri = element.getAttribute('data-lazyload');
  85. if (uri) {
  86. this.set(element, uri);
  87. //element.parentNode.parentNode.setAttribute('data-lazyload', 'true'); //debug
  88. }
  89. },
  90. destroy: function() {
  91. this._super();
  92. this.element.removeAttribute('data-imageLazyload');
  93. }
  94. });
  95. $.fn.imageLazyload = function(options) {
  96. var lazyloadApis = [];
  97. this.each(function() {
  98. var self = this;
  99. var lazyloadApi = null;
  100. if (self === document || self === window) {
  101. self = document.body;
  102. }
  103. var id = self.getAttribute('data-imageLazyload');
  104. if (!id) {
  105. id = ++$.uuid;
  106. $.data[id] = lazyloadApi = new ImageLazyload(self, options);
  107. self.setAttribute('data-imageLazyload', id);
  108. } else {
  109. lazyloadApi = $.data[id];
  110. }
  111. lazyloadApis.push(lazyloadApi);
  112. });
  113. return lazyloadApis.length === 1 ? lazyloadApis[0] : lazyloadApis;
  114. }
  115. })(mui, window, document);