我终于写了一个小脚本,终于没那么难了:)
我猜功能可以优化,我不是jquery专家...
无论如何,它为我完成了工作:获取所有图像,给它们相同的宽度,并使它们适合列的总高度。
精度:在我的例子中,我用它在 PDF 上很好地显示照片,所以我得到了与我的 A4 pdf 内容相对应的固定宽度和高度值。
它适用于您尝试显示的任何数量的图像。
JS:
function setImagesToFillAColumn() {
var maxWidth = 735;
var totalHeight = 0;
var availableHeight = 980;
//set all images to this max width and calculate total height
$('.pdf-a-column-images-container > img').each(function(){
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
var ratio = height / width;
var newHeight = maxWidth * ratio;
$(this).removeAttr("width").removeAttr("height"); // Remove HTML attributes
$(this).css("height", newHeight+'px').css("width", maxWidth+'px'); // change css attributes
totalHeight = totalHeight + newHeight;
});
//calculate the reduction purcentage neddded for all the images fit to the row
var purcentageReduction = availableHeight / totalHeight;
if (purcentageReduction < 1) {
//reduce to this purcentage for all image size
$('.pdf-a-column-images-container > img').each(function(){
var finalHeight = $(this).height() * purcentageReduction;
var finalWidth = $(this).width() * purcentageReduction;
$(this).css("height", finalHeight+'px').css("width", finalWidth+'px'); // change css attributes
});
}
}
还有html,很简单:
<div class="pdf-a-column-images-container">
<img src="urltoimg1" />
<img src="urltoimg2" />
<img src="urltoimg3" />
<img src="urltoimg4" />
</div>
为了另一种用途,我做了同样的功能,但将图像放在一行(水平)而不是一列。
它在一行中显示我给出的图像,高度相同,占行的 100%。
JS:
function setImagesToFillARow() {
var maxHeight = null;
var totalWidth = 0;
var availableWidth = 735;
//get the less high image height
$('.pdf-a-row-images-container > img').each(function(){
if (maxHeight === null || $(this).height() < maxHeight ) {
maxHeight = $(this).height();
}
});
//set all images to this height and calculate total width
$('.pdf-a-row-images-container > img').each(function(){
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
var ratio = height / width;
var newWidth = maxHeight / ratio;
$(this).removeAttr("width").removeAttr("height"); // Remove HTML attributes
$(this).css("width", newWidth+'px').css("height", maxHeight+'px'); // change css attributes
totalWidth = totalWidth + newWidth;
});
//calculate the reduction purcentage neddded for all the images fit to the row
var purcentageReduction = availableWidth / totalWidth;
var finalHeight = maxHeight * purcentageReduction;
//set images container to the new height
$('.pdf-a-row-images-container').css('height', finalHeight+'px');
//reduce to this purcentage all image size
$('.pdf-a-row-images-container > img').each(function(){
var finalWidth = $(this).width() * purcentageReduction;
$(this).css("width", finalWidth+'px').css("height", finalHeight+'px'); // change css attributes
});
}
还有html:
<div class="pdf-a-row-images-container">
<img src="urltoimg1" />
<img src="urltoimg2" />
<img src="urltoimg3" />
<img src="urltoimg4" />
</div>
对于finish,很少有css规则,用于列和行显示:
.pdf-a-row-images-container {
white-space: nowrap;
line-height: 0;
font-size: 3px;
}
.pdf-a-column-images-container {
white-space: nowrap;
line-height: 1px;
padding-top: 25px;
}
.pdf-a-column-images-container img {
display: block;
margin: 1px auto;
padding: 0;
}
我知道它远非完美,但如果它可以帮助:)