最近在做一个类似于相框的东西
但是相框的宽度和高度是不固定的
所以,希望相框中的图片等比例压缩。
按照宽度等比例显示,按照高度等比例显示
求大神,支招
就还100分,敬上
------解决方案--------------------
function proDownImage(path,imgObj) { // 等比压缩图片工具
//var proMaxHeight = 185;
var proMaxHeight=300;
var proMaxWidth = 175;
var size = {};
var image = new Image();
image.onload=
function() { // 当加载状态改变时执行此方法,因为img的加载有延迟
if (image.readyState == "complete") { // 当加载状态为完全结束时进入
if (image.width > 0 && image.height > 0) {
var ww = proMaxWidth / image.width;
var hh = proMaxHeight / image.height;
var rate = (ww < hh) ? ww: hh;
if (rate <= 1) {
alert("imgage width*rate is:" + image.width * rate);
size.width = image.width * rate;
size.height = image.height * rate;
} else {
alert("imgage width is:" + image.width);
size.width = image.width;
size.height = image.height;
}
}
}
imgObj.style.width=size.width+"px";
imgObj.style.height=size.height+"px";
};
image.src = path;
}
------解决方案--------------------
设置max-width或者max-height试试 这个好像设置了高或宽会自己等比变化吧?
------解决方案--------------------
3楼正解
img 里 设置max-width:100% 和 max-height: 100%
------解决方案--------------------
LZ可以试试看下面这段:
var resize = function(img, maxh, maxw) {
var ratio = maxh/maxw;
if (img.height/img.width > ratio){
// height is the problem
if (img.height > maxh){
img.width = Math.round(img.width(maxh/img.height));
img.height = maxh;
}