// jquery.autoimg.js v0.3 // tang bin - http://planeart.cn/ - mit licensed (function ($) { var // 设置加载状态的替换图像 temppath = './images/loading.png', // 设置加载错误的替换图像 errorpath = './images/error.png', // 检测是否支持css2.1 max-width属性 ismaxwidth = 'maxwidth' in document.documentelement.style, // 检测是否ie7浏览器 isie7 = !-[1,] && !('prototype' in image) && ismaxwidth; new image().src = temppath; $.fn.autoimg = function () { var $this = this, // 获取容器宽度 maxwidth = $this.width(); return $this.find('img').each(function (i, img) { // 如果支持max-width属性则使用此,否则使用下面预加载方式 if (ismaxwidth) return img.style.maxwidth = maxwidth + 'px'; var path = img.getattribute('data-src') || img.src, next = img.nextsibling, parent = img.parentnode, temp = new image(); // 删除img图像,并替换成loading图片 img.style.display = 'none'; img.removeattribute('src'); img.parentnode.removechild(img); temp.src = temppath; next ? next.insertbefore(temp) : parent.appendchild(temp); // 图片尺寸就绪执行 imgready(path, function (width, height) { if (width > maxwidth) { // 等比例缩放 height = maxwidth / width * height, width = maxwidth; // 删除loading图像 temp.parentnode.removechild(temp); // 恢复显示调整后的原图像 img.style.display = ''; img.style.width = width + 'px'; img.style.height = height + 'px'; img.setattribute('src', path); next ? next.insertbefore(img) : parent.appendchild(img); }; }, function () { // 加载错误 temp.src = errorpath; temp.title = 'image load error!'; }); }); }; // ie7缩放图片会失真,采用私有属性通过三次插值解决 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); // 图片头数据加载就绪事件 // http://www.planeart.cn/?p=1121 // @param {string} 图片路径 // @param {function} 获取尺寸的回调函数 (参数1接收width;参数2接收height) // @param {function} 加载错误的回调函数 (可选) (function () { var list = [], intervalid = null, tick = function () { var i = 0; for (; i < list.length; i++) { list[i].end ? list.splice(i--, 1) : list[i](); }; !list.length && stop(); }, stop = function () { clearinterval(intervalid); intervalid = null; }; this.imgready = function (url, callback, error) { var check, end, width, height, offsetwidth, offsetheight, div, accuracy = 1024, doc = document, container = doc.body || doc.getelementsbytagname('head')[0], img = new image(); img.src = url; if (!callback) return img; // 如果图片被缓存,则直接返回缓存数据 if (img.complete) return callback(img.width, img.height); // 向页面插入隐秘图像,用来监听图片是否占位 div = doc.createelement('div'); div.style.csstext = 'visibility:hidden;position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden'; div.appendchild(img) container.appendchild(div); width = img.offsetwidth; height = img.offsetheight; // 完全加载完毕的事件 img.onload = function () { end(); callback(img.width, img.height); }; // 加载错误后的事件 img.onerror = function () { end(); error && error(); }; // 检测图片是否已经占位 check = function () { offsetwidth = img.offsetwidth; offsetheight = img.offsetheight; if (offsetwidth !== width || offsetheight !== height || offsetwidth * offsetheight > accuracy) { end(); callback(offsetwidth, offsetheight); }; }; check.url = url; // 操作结束后进行清理 // 删除元素与事件,避免ie内存泄漏 end = function () { check.end = true; img.onload = img.onerror = null; div.innerhtml = ''; div.parentnode.removechild(div); }; // 将检测图片是否占位的函数加入定时器列队定期执行 // 同一图片只加入一个检测器 // 无论何时只允许出现一个定时器,减少浏览器性能损耗 !check.end && check(); for (var i = 0; i < list.length; i ++) { if (list[i].url === url) return; }; if (!check.end) { list.push(check); if (!intervalid) intervalid = setinterval(tick, 150); }; }; })(); })(jquery);