javascript - jQuery 将具有特定类别的所有图像调整为正方形

标签 javascript jquery css

我的页面内容是动态创建的,我正在尝试编写一个脚本来搜索容器“image-rounded”中的所有图像,如果它们不是正方形,则更改高度或宽度以使它们成为正方形(基于在最小边的大小上)。

到目前为止,这是我的脚本:

jQuery(function($) { 
  $('.wrapper-site').find('.image-rounded img').each(function () { 
    var round_image_width = $(this).width();
    var round_image_height = $(this).height();

if(round_image_height > round_image_width) {
  $(this).css('height', 'round_image_width');
} else {
  $(this).css('width', 'round_image_height');
}

  });  
});

但目前这什么都不做。

最佳答案

$(this).css('height', 'round_image_width'); 

应该是

$(this).css('height', round_image_width);

同样的高度......发生在我们最好的:)

关于javascript - jQuery 将具有特定类别的所有图像调整为正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251431/

相关文章:

jquery - 使用 jQuery 切换样式,但不要全部更改

javascript - 在每次迭代中附加到 dom 或创建一个数组并输出?

javascript - 在滚动点上使侧边栏淡入

jQuery 关闭按钮效果/样式

jquery - 单击时使用 jquery 隐藏元素在 IE 上不起作用?

jquery - 具有视差的背景图像

html - 使用 CSS 仅针对 Dolphin 浏览器

javascript - 在 100% 容器的页面上注册滚动

javascript - 如何像Medium一样进行拖放?

javascript - AngularJS:如何将数据获取到对象和数组中?