//设置图片宽度 function setimgwidth(curcominner, cropping, comcolumn, imgpadding, imgmargin, columnwidth, columnheight) { var imglist = curcominner.find('div.gallery-img-in img'); var setimgcss = function (curimgitem) { var imgwidth = curimgitem.width(); if (!(imgwidth > 0)) return false; var imgheight = curimgitem.height(); var widththan = 100; var leftthan = 0; var topthan = 0; //正常格式 if (cropping == 0) { var temmargin = 0; var tempadding = 0; if (imgpadding > 0) tempadding += (imgpadding + 1) * 2; if (imgmargin > 0) temmargin += imgmargin * 2; var newcolumnwidth = columnwidth - temmargin; var newcolumnheight = columnheight - temmargin; if (imgwidth <= newcolumnwidth && imgheight <= newcolumnheight) { widththan = 'auto'; leftthan = (((columnwidth - imgwidth) / 2) / columnwidth) * 100; topthan = (((columnheight - imgheight) / 2) / columnheight) * 100; } else { if (imgheight > newcolumnheight) { imgwidth = (newcolumnheight / imgheight) * imgwidth; imgheight = newcolumnheight; } if (imgwidth > columnwidth) { imgheight = (columnwidth / imgwidth) * imgheight; imgwidth = columnwidth; } widththan = ((imgwidth / columnwidth) * 100) + '%'; leftthan = (((columnwidth - imgwidth) / 2) / columnwidth) * 100; topthan = (((columnheight - imgheight) / 2) / columnheight) * 100; } curimgitem.css({'width': widththan, 'left': leftthan + '%', 'top': topthan + '%'}); } else { //正方形和长方形 var temimgwidth = columnwidth - (imgpadding + 1) * 2; var imgnewheight = imgheight * (temimgwidth / imgwidth); if (imgnewheight < columnheight) { imgwidth = (columnheight / imgheight) * imgwidth; widththan = (imgwidth / columnwidth) * 100; leftthan = 0 - (((imgwidth - columnwidth) / 2) / columnwidth) * 100; } else { imgheight = imgnewheight; topthan = 0 - ((((imgheight - columnheight) / 2) / columnheight) * 100); } curimgitem.css({'width': widththan + '%', 'left': leftthan + '%', 'top': topthan + '%'}); } } // 判断图片加载的函数 var newisimgload = function (_obj) { if (_obj.height() === 0) { var t_img; t_img = setinterval(function () { clearinterval(t_img); newisimgload(_obj); }, 10); } else { setimgcss(_obj); } } for (var i = 0; i < imglist.length; i++) { var temimg = $(imglist[i]); temimg.css({'width': 'auto'}); var newimg = new image; newimg.index = i; newisimgload($(imglist[i])); newimg.src = temimg.attr('src'); } }