使用jQuery实现指定图片按比例缩放
2011.08.23 update:
$('.entry img').each(function(){
var width=710; //输入指定宽度,图片高度自动适应
var thiswidth = parseInt($(this).css('width'));
var thisheight = parseInt($(this).css('height'));
if(thiswidth < width)
$(this).css('height',width / thiswidth * thisheight).css('width',width);
});
-----------------------------------------以下为旧代码,不推荐使用-----------------------------------------
$('.entry img').each(function(){
var width=560; //输入指定宽度,图片高度自动适应
var image = $(this);
if(image.width()>width){
image.height(width/image.width()*image.height());
image.width(width);
image.wrap("<a href='"+image.attr("src")+"' target='_blank'></a>").css("border","0");
}
});
var width=560; //输入指定宽度,图片高度自动适应
var image = $(this);
if(image.width()>width){
image.height(width/image.width()*image.height());
image.width(width);
image.wrap("<a href='"+image.attr("src")+"' target='_blank'></a>").css("border","0");
}
});