gpt4 book ai didi

javascript - 将不同的图像强制设置为相同的高度,以便组合宽度填充固定的 div

转载 作者:行者123 更新时间:2023-11-28 09:04:54 25 4
gpt4 key购买 nike

过去几个小时我一直在寻找一种方法来简化我目前手动执行的操作。我有将所有图像缩放到 500 像素宽度的 div。这通常很好,但我的一些图像太小而无法缩放到那么宽。

所以我试图将这些较小的图像并排放置在一个新的 500 像素宽的 div 中。这些较小的图像通常彼此具有不同的高度/宽度比,因此我强制它们的高度相等(+/- 1 像素),以便它们的组合宽度为 500 像素。这使外边缘与它们上方或下方的单个图像的边缘完全对齐。在这个例子中,我像这样手动设置每个图像高度:

    <!-- SIDE BY SIDE DIV -->    
<div style="width:500px;">
<!-- IMAGE 01 -->
<div class="postContainer" id="imgContainer">
<a href="http://example.com/example.jpg" imageanchor="1">
<img height="248px" src="http://example.com/example.jpg" /></a>
<div class="postTextbox">
<div class="postText">Snappy comment.</div>
</div>
</div>
<!-- IMAGE 02 -->
<div class="postContainer" id="imgContainer">
<a href="http://example.com/example.jpg" imageanchor="1">
<img height="248px" src="http://example.com/example.jpg" /></a>
<!-- <div class="postTextbox"><div class="postText">NO COMMENT.</div></div> --></div>
</div>
<!-- END SIDE BY SIDE DIV -->

JSFiddle Example.

我想弄清楚是否有更自动化的方法(也许通过脚本?)来找到“神奇”的高度数字,这将导致两个图像的宽度等于 500px,因为通过猜测和检查手动找到它或者简单的数学变得有点麻烦。

如果这是一个重复的问题,我深表歉意,但我就是找不到答案。

非常感谢您的指导!

更新:

我一直在根据 Lathejockey81 的示例对此进行一些研究,我认为我已经开始工作,因此脚本仅针对特定的 div 类,因此您可以决定单独显示哪些图像以及将哪些图像组合在一起。您还可以并排组合两个以上的图像。但是,目前它似乎只能在 Chrome 中正常运行......

var x2 = document.getElementsByClassName('x2');
var divWidth = 500
var imgPad = 4
var pairs = []

function addUp(val, maxh, maxW) {
var theWidths = 0;
for (j = 0; j < val.length; j++) {
theWidths += Math.round(val[j].naturalWidth * (maxh / val[j].naturalHeight));
}
return theWidths;
}
// Find img tags in x2 divs & covert to array
for (var i = 0; i < x2.length; i++) {
var pair = x2[i].getElementsByTagName('img');
var arr = [].slice.call(pair);
pairs.push(arr);
}

for (x = 0; x < pairs.length; x++) {
var pairsX = pairs[x],
pairsXlen = pairsX.length;
// Adds padding depending on # of images
var maxWidth = divWidth - (imgPad * (pairsXlen - 1));
var addWidths = 0;
var startHeight = 20;
for (var i = 0; addWidths < maxWidth; i++) {
// Step heights up by 1
newHeight = startHeight + i;
for (y = 0; y < pairsXlen; y++) {
pairsX[y].style.height = newHeight + 'px';
// Add up the widths
addWidths = addUp(pairsX, newHeight, maxWidth);
// Jump out when width's exceeded
if (addWidths > maxWidth) {
break}
}
}
}

因为我从来没有写过 javascript,所以我很感激任何关于可以做些什么来改进这个脚本的反馈,特别是为什么它在 FF 或 IE 中不能正常工作。

再次感谢您的关注!

更新#2:

这似乎是样式问题。

.widthVal {
width: 500px;
overflow-x:hidden;
white-space:nowrap;
}

有了这个,图像就不会在 FireFox 或 IE 中跳到下一行。尽管最外面的图像边缘在 IE 中不是相当像素完美对齐,但它们对我来说已经足够接近了。

我后来了解到“center”标签已被弃用,所以我也将删除它!

这是新的工作示例:Example working in Big 3 browsers.

最佳答案

我更新了你的 fiddle 以演示解决方案 here .

您需要对所有图像进行排序,并将瘦的图像配对,然后您可以计算出它们的新高度。

var images = document.getElementsByTagName('img');

var tooSmall = []; // Collection of smallPairs
var smallPair = []; // a pair of images to shrink

// Calculates width after first shrink
var newWidth = function (image, newHeight) {
return image.naturalWidth * (newHeight/image.naturalHeight);
};

// If a single image is too narrow, grab the next regardless of its width
for(var i=0;i<images.length;i++) {
if (smallPair.length !== 1 && (images[i].naturalWidth > 500 || smallPair.length > 1)) {
// Shrink any image too wide
if(images[i].naturalWidth > 500) {
images[i].setAttribute('width','500px');
}
if(smallPair.length > 1) {
tooSmall.push(smallPair);
}
smallPair = [];
}
else {
smallPair.push(images[i]);
}
}

// Loop through image pairs and shrink them
for(var i=0;i<tooSmall.length;i++) {
var newHeight = tooSmall[i][0].naturalHeight;
if(tooSmall[i][1].naturalHeight < newHeight) {
newHeight = tooSmall[i][1].naturalHeight;
}
var totalWidth = newWidth(tooSmall[i][0], newHeight) + newWidth(tooSmall[i][1], newHeight);
var sizeRatio = 496 / totalWidth; // Use 496 width to account for padding
newHeight = Math.round(newHeight * sizeRatio);
tooSmall[i][0].setAttribute('height', newHeight + 'px');
tooSmall[i][1].setAttribute('height', newHeight + 'px');
}

如果您想要制作静态页面,可能有更简单的解决方案,但这两种方法都应该有效。如果您要动态加载内容,还有服务器端选项。

关于javascript - 将不同的图像强制设置为相同的高度,以便组合宽度填充固定的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com