// jquery.imgautocenter - 2012-03-14 - hogen wang // plus jquery.autoimg.js - 2010-04-02 - tang bin (function ($) { // 检测是否支持css2.1 max-width属性 var ismaxwidth = 'maxwidth' in document.documentelement.style, // 检测是否ie7浏览器 isie7 = !-[1,] && !('prototype' in image) && ismaxwidth; $.fn.autoimg = function () { var maxwidth = this.width(); return this.find('img').each(function (i, img) { // 如果支持max-width属性则使用此,否则使用下面方式 if (ismaxwidth) return img.style.maxwidth = maxwidth + 'px'; var src = img.src; // 隐藏原图 img.style.display = 'none'; img.removeattribute('src'); // 获取图片头尺寸数据后立即调整图片 imgready(src, function (width, height) { // 等比例缩小 if (width > maxwidth) { height = maxwidth / width * height, width = maxwidth; img.style.width = width + 'px'; img.style.height = height + 'px'; }; // 显示原图 img.style.display = ''; img.setattribute('src', src); }); }); }; $.fn.imgautocenter = function (settings) { settings=jquery.extend({ imgmaxwidth:0, imgmaxheight:0 },settings); var maxwidth = this.width(); var maxheight = this.height(); var panel= this; //alert(panel.html()); this.find('img').each(function (i, img) { // 如果支持max-width属性则使用此,否则使用下面方式 if (ismaxwidth){ img.style.maxwidth = maxwidth + 'px'; //img.style.margintop = //alert(img.height); //return; } var src = img.src; var imgitem =$(this); if(settings.imgmaxwidth == 0) settings.imgmaxwidth = maxwidth; if(settings.imgmaxheight == 0) settings.imgmaxheight = maxheight; // 隐藏原图 //img.style.display = 'none'; //img.removeattribute('src'); // 获取图片头尺寸数据后立即调整图片 imgready(src, function (imgw, imgh) { // 等比例缩小 //alert((settings.imgmaxwidth / settings.imgmaxheight) > (imgw / imgh)); //如果比例大于设置的,说明图片比较"长"imgmaxwidth/imgmaxheight)>(imgw/imgh) if ((settings.imgmaxwidth / settings.imgmaxheight) > (imgw / imgh)) { if (settings.imgmaxheight > imgh) { //图片比例小于最大设定,则居中即可 var margintop = (settings.imgmaxheight - imgitem.height()) / 2; if (margintop < 0) { margintop = 0; } //img.style.margintop = margintop + "px"; imgitem.css("margin-top", margintop + "px"); panel.css("text-align", "center"); } else { imgitem.css("height", settings.imgmaxheight + "px"); panel.css("text-align", "center"); } } else { //图片比较窄,需要缩放到最大宽度 if (settings.imgmaxwidth > imgw) { var margintop = (settings.imgmaxheight - imgitem.height()) / 2; if (margintop < 0) { margintop = 0; } imgitem.css("margin-top", margintop + "px"); panel.css("text-align", "center"); } else { imgitem.css("width", settings.imgmaxwidth + "px"); var margintop = (settings.imgmaxheight - (imgh*settings.imgmaxwidth/imgw)) / 2; imgitem.css("margin-top", margintop + "px"); } } // 显示原图 //img.style.display = ''; //img.setattribute('src', src); }); }); }; // 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); /** * 图片头数据加载就绪事件 * @see http://www.planeart.cn/?p=1121 * @param {string} 图片路径 * @param {function} 尺寸就绪 (参数1接收width; 参数2接收height) * @param {function} 加载完毕 (可选. 参数1接收width; 参数2接收height) * @param {function} 加载错误 (可选) */ var imgready = (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; }; return function (url, ready, load, error) { var check, width, height, newwidth, newheight, img = new image(); img.src = url; // 如果图片被缓存,则直接返回缓存数据 if (img.complete) { ready(img.width, img.height); load && load(img.width, img.height); return; }; // 检测图片大小的改变 width = img.width; height = img.height; check = function () { newwidth = img.width; newheight = img.height; if (newwidth !== width || newheight !== height || // 如果图片已经在其他地方加载可使用面积检测 newwidth * newheight > 1024 ) { ready(newwidth, newheight); check.end = true; }; }; check(); // 加载错误后的事件 img.onerror = function () { error && error(); check.end = true; img = img.onload = img.onerror = null; }; // 完全加载完毕的事件 img.onload = function () { load && load(img.width, img.height); !check.end && check(); // ie gif动画会循环执行onload,置空onload即可 img = img.onload = img.onerror = null; }; // 加入队列中定期执行 if (!check.end) { list.push(check); // 无论何时只允许出现一个定时器,减少浏览器性能损耗 if (intervalid === null) intervalid = setinterval(tick, 40); }; }; })(); })(jquery);