//设置图片宽度 function setimgwidth(curcominner, cropping, comcolumn, imgpadding, imgmargin, columnwidth, columnheight, imgborder) { 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 heightthan = 100; var temborder=0; var leftthan = 0; var topthan = 0; var temmargin = 0; var tempadding = 0; if (imgborder > 0){ temborder = imgborder; } if (imgpadding > 0){ tempadding += ( parseint(imgpadding) + parseint(temborder)) * 2; } if(!cropping){ tempadding=0; } //if (imgmargin > 0){ // temmargin += parseint(imgmargin) * 2; // } var newcolumnwidth = columnwidth - temmargin - tempadding; var newcolumnheight = columnheight; //正常格式 if (cropping == 0) { newcolumnwidth = columnwidth - temmargin; imgwidth += tempadding; imgheight += tempadding; if (imgwidth <= newcolumnwidth && imgheight <= newcolumnheight) { widththan = 'auto'; heightthan = 'auto'; leftthan = (((newcolumnwidth - imgwidth) / 2) / newcolumnwidth) * 100; topthan = (((newcolumnheight - imgheight) / 2) / newcolumnheight) * 100; } else { if (imgheight > newcolumnheight) { imgwidth = (newcolumnheight / imgheight) * imgwidth; imgheight = newcolumnheight; } if (imgwidth > newcolumnwidth) { imgheight = (newcolumnwidth / imgwidth) * imgheight; imgwidth = newcolumnwidth; } widththan = ((imgwidth / newcolumnwidth) * 100) + '%'; heightthan = ((imgheight / newcolumnheight) * 100) + '%'; leftthan = (((newcolumnwidth - imgwidth) / 2) / newcolumnwidth) * 100; topthan = (((newcolumnheight - imgheight) / 2) / newcolumnheight) * 100; } curimgitem.css({'width': widththan, 'height': heightthan, 'left': leftthan + '%', 'top': topthan + '%'}); } else { //正方形和长方形 var imgnewheight = imgheight * (newcolumnwidth / imgwidth); if (imgnewheight < newcolumnheight) { imgwidth = (newcolumnheight / imgnewheight) * newcolumnwidth; widththan = (imgwidth / newcolumnwidth) * 100; leftthan = 0 - (((imgwidth - newcolumnwidth) / 2) / newcolumnwidth) * 100; } else { imgheight = imgnewheight; heightthan = (imgheight / newcolumnheight) * 100; topthan = 0 - ((((imgheight - newcolumnheight) / 2) / newcolumnheight) * 100); } curimgitem.css({'width': widththan + '%', 'height': heightthan + '%', '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'); } }