我終於寫了一個小腳本,不用這麼辛苦終於:)
我猜的功能,可以優化,我不是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回答,遺憾的是缺乏細節... 比例必須是尊重。 不需要圖像具有相同的高度(因爲我想保持比例) 需要圖像具有相同的寬度(列「外觀」)。 我更喜歡如果圖像完全顯示,但如果有時隱藏一小部分,那就沒問題。 我要準備幾張圖片來說明我的問題。我會回來的 ! :) –