Image resize with jquery
need jquery image resize, like if image width > 400 then use resize width:50%, other images that is smaller size like 100X100 dont need to be resized.
I use this code
var max_size = 400;
$(".comment p.message a.fancy img").each(function(i) {
if ($(this).height() > $(this).width()) {
var h = max_size;
var w = Math.ceil($(this).width() / $(this).height() * max_size);
} else {
var w = max_size;
var h = Math.ceil($(this).height() / $(this).w开发者_如何学Pythonidth() * max_size);
}
$(this).css({ height: h, width: w });
});
But this code dont check smaller images.
Try this: http://jsfiddle.net/aJffu/2/
var maxsize = 400;
$("#images img").each(function() {
var t = $(this);
var maxdim = Math.max(t.width(), t.height());
if (maxdim > maxsize) {
t.css({
width: t.width() / 2,
height: t.height() / 2
});
}
});
This halves the size of the image if either the width or height exceeds maxsize
. Shouldn't be hard to adapt this to your specific needs. For example, this version scales the image such that the the longest dimension is maxsize
.
精彩评论