我终于写了一个小脚本,不用这么辛苦终于:)
我猜的功能,可以优化,我不是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>
而对于完成,fewcss规则,用于列和行显示:
.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;
}
我知道这是远离是完美的,但如果它可以帮助:)
而不是想象这种情况,如果你用HTML/CSS/JavaScript重新创建这个例子,这样会更好,这样我们就可以看到你有什么并且能够帮助你。 – automaton
你能澄清你的最终结果吗?你想要所有的三个图像是相同的高度/宽度?随着高度填充容器的高度?你关心被扭曲的图像,或者你期望图像仅被部分显示?像@automaton一样,你应该给我们一些代码来处理。您可以使用http://placehold.it为示例提供图像。 – wlh
Thansk回答,遗憾的是缺乏细节... 比例必须是尊重。 不需要图像具有相同的高度(因为我想保持比例) 需要图像具有相同的宽度(列“外观”)。 我更喜欢如果图像完全显示,但如果有时隐藏一小部分,那就没问题。 我要准备几张图片来说明我的问题。我会回来的 ! :) –