如何计算图像是横向还是纵向

geo*_*075 11 jquery image-gallery

我正在用jquery创建一个图库.是否有可能使用jquery计算图像是横向还是纵向?

感谢您的支持.

小智 25

您可以简单地比较图像的宽度和高度.

var someImg = $("#someId");
if (someImg.width() > someImg.height()){
    //it's a landscape
} else if (someImg.width() < someImg.height()){
    //it's a portrait
} else {
    //image width and height are equal, therefore it is square.
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*eet 6

这对我有用,使用自然高度/宽度来获取原始属性。

       function imageOrientation(src) {

            var orientation,
            img = new Image();
            img.src = src;

            if (img.naturalWidth > img.naturalHeight) {
                orientation = 'landscape';
            } else if (img.naturalWidth < img.naturalHeight) {
                orientation = 'portrait';
            } else {
                orientation = 'even';
            }

            return orientation;

        }
Run Code Online (Sandbox Code Playgroud)