1// jQuery.autoIMG.js v0.3 2(function ($) { 3 var // 设置å è½½ç¶æçæ¿æ¢å¾å 4 tempPath = '/material/images/loading.gif', 5 // 设置å è½½éè¯¯çæ¿æ¢å¾å 6 errorPath = '/material/images/error.gif', 7 // æ£æµæ¯å¦æ¯æcss2.1 max-width屿§ 8 isMaxWidth = 'maxWidth' in document.documentElement.style, 9 // æ£æµæ¯å¦IE7æµè§å¨ 10 isIE7 = !-[1,] && !('prototype' in Image) && isMaxWidth; 11 12 new Image().src = tempPath; 13 14 $.fn.autoIMG = function () { 15 var $this = this, 16 // è·å容å¨å®½åº¦ 17 maxWidth = $this.width(); 18 19 return $this.find('img').each(function (i, img) { 20 21 // å¦ææ¯æmax-width屿§åä½¿ç¨æ¤ï¼å¦å使ç¨ä¸é¢é¢å è½½æ¹å¼ 22 if (isMaxWidth) return img.style.maxWidth = maxWidth + 'px'; 23 24 var path = img.getAttribute('data-src') || img.src, 25 next = img.nextSibling, 26 parent = img.parentNode, 27 temp = new Image(); 28 29 // å é¤imgå¾åï¼å¹¶æ¿æ¢æloadingå¾ç 30 img.style.display = 'none'; 31 img.removeAttribute('src'); 32 img.parentNode.removeChild(img); 33 temp.src = tempPath; 34 next ? next.insertBefore(temp) : parent.appendChild(temp); 35 36 // å¾ç尺寸就绪æ§è¡ 37 imgReady(path, function (width, height) { 38 if (width > maxWidth) { 39 // çæ¯ä¾ç¼©æ¾ 40 height = maxWidth / width * height, 41 width = maxWidth; 42 43 // å é¤loadingå¾å 44 temp.parentNode.removeChild(temp); 45 46 // æ¢å¤æ¾ç¤ºè°æ´åçåå¾å 47 img.style.display = ''; 48 img.style.width = width + 'px'; 49 img.style.height = height + 'px'; 50 img.setAttribute('src', path); 51 next ? next.insertBefore(img) : parent.appendChild(img); 52 }; 53 }, function () { 54 // å è½½é误 55 temp.src = errorPath; 56 temp.title = 'Image load error!'; 57 }); 58 59 }); 60 }; 61 62 // IE7缩æ¾å¾çä¼å¤±çï¼éç¨ç§æå±æ§éè¿ä¸æ¬¡æå¼è§£å³ 63 isIE7 && (function (c,d,s) {s=d.createElement('style');d.getElementsByTagName('head')[0].appendChild(s);s.styleSheet&&(s.styleSheet.cssText+=c)||s.appendChild(d.createTextNode(c))})('img {-ms-interpolation-mode:bicubic}',document); 64 65 // å¾çå¤´æ°æ®å 载就绪äºä»¶ 66 // @param {String} å¾çè·¯å¾ 67 // @param {Function} è·å尺寸çåè°å½æ° (åæ°1æ¥æ¶widthï¼åæ°2æ¥æ¶height) 68 // @param {Function} å è½½é误çåè°å½æ° (å¯é) 69 (function () { 70 var list = [], intervalId = null, 71 72 tick = function () { 73 var i = 0; 74 for (; i < list.length; i++) { 75 list[i].end ? list.splice(i--, 1) : list[i](); 76 }; 77 !list.length && stop(); 78 }, 79 80 stop = function () { 81 clearInterval(intervalId); 82 intervalId = null; 83 }; 84 85 this.imgReady = function (url, callback, error) { 86 var check, end, width, height, offsetWidth, offsetHeight, div, 87 accuracy = 1024, 88 doc = document, 89 container = doc.body || doc.getElementsByTagName('head')[0], 90 img = new Image(); 91 92 img.src = url; 93 if (!callback) return img; 94 95 // 妿å¾ç被ç¼åï¼åç´æ¥è¿åç¼åæ°æ® 96 if (img.complete) return callback(img.width, img.height); 97 98 // åé¡µé¢æå ¥éç§å¾åï¼ç¨æ¥çå¬å¾çæ¯å¦å ä½ 99 div = doc.createElement('div'); 100 div.style.cssText = 'visibility:hidden;position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden'; 101 div.appendChild(img) 102 container.appendChild(div); 103 width = img.offsetWidth; 104 height = img.offsetHeight; 105 106 // å®å ¨å è½½å®æ¯çäºä»¶ 107 img.onload = function () { 108 end(); 109 callback(img.width, img.height); 110 }; 111 112 // å è½½é误åçäºä»¶ 113 img.onerror = function () { 114 end(); 115 error && error(); 116 }; 117 118 // æ£æµå¾çæ¯å¦å·²ç»å ä½ 119 check = function () { 120 offsetWidth = img.offsetWidth; 121 offsetHeight = img.offsetHeight; 122 if (offsetWidth !== width || offsetHeight !== height || offsetWidth * offsetHeight > accuracy) { 123 end(); 124 callback(offsetWidth, offsetHeight); 125 }; 126 }; 127 check.url = url; 128 129 // æä½ç»æåè¿è¡æ¸ ç 130 // å é¤å ç´ ä¸äºä»¶ï¼é¿å IEå åæ³æ¼ 131 end = function () { 132 check.end = true; 133 img.onload = img.onerror = null; 134 div.innerHTML = ''; 135 div.parentNode.removeChild(div); 136 }; 137 138 // å°æ£æµå¾çæ¯å¦å ä½ç彿°å å ¥å®æ¶å¨åé宿æ§è¡ 139 // åä¸å¾çåªå å ¥ä¸ä¸ªæ£æµå¨
140 // æ è®ºä½æ¶åªå 许åºç°ä¸ä¸ªå®æ¶å¨ï¼åå°æµè§å¨æ§è½æè 141 !check.end && check(); 142 for (var i = 0; i < list.length; i ++) { 143 if (list[i].url === url) return; 144 }; 145 if (!check.end) { 146 list.push(check); 147 if (!intervalId) intervalId = setInterval(tick, 150); 148 }; 149 }; 150 })(); 151 152})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.