需求是傳過來一個圖片,根據外層div的大小自動進行縮放效果。
function ShowSecondImg(v) { var rate, newX, newY,newW, newH = 0; //表示圖片相對窗口的縮放比例 var imgW, imgH; var centerW = $(window).width() - $("#alarmQueue").width() - $("#presInfo").width() - 40; var centerH = $(window).height() - $("#detailedInfor").height()-148; $("#imgBoxs").find("#second").remove(); var img = $('<img/>', { src: v.First_frame_image_url, "id": "second" });//v.First_frame_image_url img.prependTo('#imgBoxs'); imgW = img.width(); imgH = img.height(); //獲取的參數 var rectX= parseInt(v.facerect.x); var rectY =parseInt(v.facerect.y); var rectWidth =parseInt(v.facerect.w); var rectHeight =parseInt(v.facerect.h); // alert(v.facerect.x); //var i = (centerW / 16 - centerH / 9) > 0 ? 0 : 1; var i= (centerW/centerH - imgW/imgH ) > 0 ? 0 : 1; if (i == 1) { //如果窗口寬不長,高長。以寬為准。 rate = imgW / centerW; var imgRateH = imgH / rate; //img real rate height; newX = rectX / rate newY = rectY / rate; newW = rectWidth / rate; newH = rectHeight / rate; $("#imgBox").width(centerW); $("#imgBox").css("height", "100%"); $("#second").css({ "width": centerW, "height": imgRateH }); } else if (i == 0) { //如果窗口寬足夠長,高不長。以高為准。 rate = centerH / imgH; var imgRateW = imgW * rate; //img real rate width; var leftsideW = (centerW - imgRateW) / 2; //leftside add rightside width; newX = rectX * rate + leftsideW; newY = rectY * rate; newW = rectWidth * rate; newH = rectHeight * rate; $("#imgBox").height(centerH); $("#imgBox").css("width", "100%"); $("#second").css({ "width": imgRateW, "height": centerH }); } // $("#detailedInfor").height($(".container").height()-centerH); $("#faceDiv").attr('style', 'display:block;top:' + newY + 'px;left:' + newX + 'px;width:' + newW + 'px;height:' + newH + 'px'); }