jQuery函数将一组图像调整为给定区域(不是高度或宽度) [英] jQuery function to resize a set of images to a given area (not height or width)
本文介绍了jQuery函数将一组图像调整为给定区域(不是高度或宽度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个jQuery函数,它将根据指定的区域调整一组图像的大小,而不是简单地调整最大高度或宽度。
I'm trying to write a jQuery function that will resize a set of images according to a specified area, rather than simply a max height or width.
这里有一个类似的问题:调整图片大小区域,但我想让它在jquery中工作并同时使用多个图像
There's a similar question here: resize image by area, but I'd like to get it working in jquery and with multiple images at the same time
以下是我目前正在使用的内容:< a href =http://jsfiddle.net/szSE5/21/ =nofollow noreferrer> http://jsfiddle.net/szSE5/21/ - 它的运行方式不符合我的预期那一刻。
Here's what I'm currently working with: http://jsfiddle.net/szSE5/21/ — it's not functioning the way I intended at the moment.
推荐答案
这个怎么样:
jQuery.fn.resizeImgByArea = function(avgDimension){
var $this = $(this),
oldW = $this.width(),
oldH = $this.height(),
RatiO = new Number(oldW/oldH),
newH = new Number(Math.round(Math.sqrt(avgDimension/RatiO))),
newW = new Number (Math.round(newH * RatiO));
$this.css({
width: newW + 'px',
height: newH + 'px'
});
}
$(document).ready(function() {
$('#images img').each(function(){$(this).resizeImgByArea(10000)});
});
这篇关于jQuery函数将一组图像调整为给定区域(不是高度或宽度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文