【问题标题】:Resize tall images to the size of other images in flexbox grid将 tall 图像的大小调整为 flexbox 网格中其他图像的大小
【发布时间】:2016-11-28 05:53:21
【问题描述】:

所以我在 Wordpress 类别页面上有一个 flexbox 网格,显示我所有带有标题和摘录的帖子。大多数图像的比例相同,除了少数更高。我想响应地将高图像的最大高度设置为常规图像的高度。网格中的每个项目都有一个带有 imageThumb 类的图像容器。

(function($) {
 $('.imageThumb img').each(function() {
    var maxHeight;
    var width = $(this).clientWidth;    // Current image width
    var height = $(this).clientHeight;  // Current image height

    // Check if the current width is larger than the max
    if(width > height){
        maxHeight = $(this).clientHeight;
        console.log(maxHeight)
    }

    // Check if current height is larger than max
    if(height > width){
        $(this).css("height", maxHeight);   // Set new height
    }
});

})(jQuery);

我知道我使用 (width > height) 位返回了太多高度。我无法突出显示特定图像并获得它们的高度,因为此博客上的内容总是在变化和变化。

这是与此相关的我的 css。

.imageThumb {
  overflow: hidden;
}

.imageThumb img {
  width: 100%;
}

我不想为图像设置宽度和高度(我的原始解决方案),因为网站所有者希望图片随着窗口大小的增大而继续放大)。我还尝试将图像设置为绝对定位并将其安装在容器中,但这似乎不适用于这种情况。不确定解决此问题的最佳行动方案是什么。

提前致谢!

【问题讨论】:

  • 我相信你的问题,但我有一个疑问..为什么你检查宽度它不足以将所有图像设置为相同的高度?
  • 我不确定我是否理解您的评论,但我想您是在说我为什么不将所有图像设置为某个高度?我可以,但是当我需要做的只是为高个子设置一个最大高度时,这似乎有点过分了。
  • 确定这是我的意思检查代码

标签: jquery css wordpress grid flexbox


【解决方案1】:

Daebak Do 用一些模组引导我找到了正确的答案。我需要设置容器 imageThumb 的最大高度,以便宽度与其他图片保持一致,而不会扭曲高大的图片。如果它最终切断了您的图片太多,您可以尝试注释代码。

var maxHeight = 0;

$('.imageThumb').each(function(){
   var thisH = $(this).height();
   if (thisH > maxHeight) { 
    maxHeight = thisH;
    $('.imageThumb').css('maxHeight', maxHeight);
    //may need to change to the following if pics are getting cut off too much
    // $('.imageThumb img').css('maxHeight', maxHeight);
    // // $('.imageThumb img').css('width', 'auto');

     }
});

上面的代码并不适用于我需要做的所有事情,请参阅下面的代码。欢迎反馈!

var imageThumb = document.getElementsByClassName('imageThumb');
var arr=[];
var tallArr=[];
//add all items to an array
$(".imageThumb").each(function(){ arr.push($(this));});
//filter out all items that are taller than they are wide
arr.filter(function(obj){
  if(obj.context.clientHeight > obj.context.clientWidth) {
    tallArr.push(obj);
  } 
});


//loop through the items that are tall and add a new class to them
for (i = 0; i <= tallArr.length; i++) {
  $(tallArr[i]).addClass('bigHeight'); 
}
//gets first item that has a normal height and grabs it's height value
var normalHeight = arr[0].context.clientHeight;
//change css of bigHeight

//sets bigHeight equal to normalHeight
$('.bigHeight').css('max-height', normalHeight);

【讨论】:

    【解决方案2】:

    你好,我不知道我是否理解得很好,如果没有,请让我看看你的链接。

    顺便说一句,我认为这段代码应该可以完成这项工作

    var maxHeight = 0;
    
    $('.imageThumb img').each(function(){
       var thisH = $(this).height();
       if (thisH > maxHeight) { maxHeight = thisH; }
    });
    
    $('.imageThumb img').height(maxHeight);
    

    干杯!!

    【讨论】:

    • 这让我走上了正确的道路。在下面粘贴正确的代码。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2021-10-02
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多