Javascript 仅调整垂直图像的大小

Javascript 仅调整垂直图像的大小,javascript,jquery,css,image,image-resizing,Javascript,Jquery,Css,Image,Image Resizing,我有一个垂直滚动照片库的问题, 我想调整垂直图像的大小,但水平图像是罚款的方式,他们是。 水平图像的宽度为900px,垂直图像对于舒适的屏幕观看来说太高了,所以我想要两个440px宽度的垂直图像,中心边缘为20px,以适合一个水平图像的下方 该网站位于CargoCollection上,所以我不能使用PHP,只能使用Jquery、Javascript和CSS 我只能添加HTML。 有人有办法吗 检测图像比率,然后仅在高度>宽度时调整大小的方法 谢谢 在jQuery1.7及更高版本中,我们可以访问每

我有一个垂直滚动照片库的问题, 我想调整垂直图像的大小,但水平图像是罚款的方式,他们是。 水平图像的宽度为900px,垂直图像对于舒适的屏幕观看来说太高了,所以我想要两个440px宽度的垂直图像,中心边缘为20px,以适合一个水平图像的下方

该网站位于CargoCollection上,所以我不能使用PHP,只能使用Jquery、Javascript和CSS 我只能添加HTML。 有人有办法吗

检测图像比率,然后仅在高度>宽度时调整大小的方法

谢谢

在jQuery1.7及更高版本中,我们可以访问每个图像的属性,而无需使用$.each迭代器

$('img').prop('height', function(){
    if($(this).height() > $(this).width()){
        //portrait, resize accordingly  
    }else{
       //landscape display; the default you want.
    }
});

OhGodwhy答案的变体,确保在计算高度/宽度时加载图像

$('#myElement img').load(function(){
    if($(this).height() > $(this).width()){
        //portrait, resize accordingly  
        var width = $(this).width();
        var height = $(this).height();
        var newWidth = 400;
        var newHeight = newWidth * (height / width);
        $(this).width(newWidth).height(newHeight);
    }else{
        //landscape display; the default you want.
    }
});
也可以使用
$(this)
而不是通过
obj
,但我想这只是个人偏好!
$('#myElement img').load(function(){
    if($(this).height() > $(this).width()){
        //portrait, resize accordingly  
        var width = $(this).width();
        var height = $(this).height();
        var newWidth = 400;
        var newHeight = newWidth * (height / width);
        $(this).width(newWidth).height(newHeight);
    }else{
        //landscape display; the default you want.
    }
});