正文 1515字数 199,162阅读

function setImgSizeLoad(e){ var img = $(e); var div = $("<div></div>"); img.parent().append(div); div.append(e); //获取限制容器的长宽。 var maxWidth = img.parent().parent().width(); if(maxWidth == 0){ maxWidth = img.width(); } var maxHeight = img.parent().parent().height(); if(maxHeight == 0){ maxHeight = img.height(); } //设置容器的长宽。和样式 div.width(maxWidth+"px"); div.height(maxHeight+"px"); div.css("overflow","hidden"); div.css("background-color","gray"); //解除图片的长宽限制 img.height("auto"); img.width("auto"); var imgWidth = img.height(); var imgHeight = img.height(); //如果图片的长和宽大于现在的长宽就进行缩放 if(img.width() > maxWidth && img.height() > maxHeight){ img.width(maxWidth+"px"); if(img.height() < maxHeight){ img.width("auto"); img.height(maxHeight+"px"); } } img.css("margin-left",(maxWidth - img.width())/2 +"px"); img.css("margin-top",(maxHeight - img.height())/2 +"px"); } function setImgSize(e){ var timer = setInterval(function(){ if(e.complete){ setImgSizeLoad(e) clearInterval(timer); }else{ alert("aaaa"); } },50) };
Run code
Cut to clipboard