最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Check aspect ratio of img via Javascript - Stack Overflow

programmeradmin4浏览0评论

I've been asking a css viable solution in this question for a responsive photo gallery with images of different ratios.
Unfortunately i guess it's too complicated to do this via css, so the only fast-easy solution is to use javascript to check the thumbnails aspect ratio and change the css inline for certain thumbs:
the script should check the height:width ratio of every thumbnail, if the ratio is lower than 0.6666666666666667 (2:3) then set via css height:100%; max-width:none; to override the actual rules. Can also be added a class, if easier.

How can this be done via javascript? (jquery can be used too as the library is already present for other plugins).

The structure of the grid is like this:

<ul id="gallery" class="gallery-grid">
  <li class="grid-block">
    <div class="block-wrap">
        <div class="link-wrap">
            <a href="<?php echo $photo->sourceImageFilePath; ?>" class="block-link" target="_blank" >
                <img class="block-thumb" src="<?php echo $photo->thumbImageFilePath; ?>"/>              
            </a>
        </div>  
    </div>
  </li>
</ul>

Ofcourse if you can find a viable css answer to my previous question is even more welcome! thanks!

I've been asking a css viable solution in this question for a responsive photo gallery with images of different ratios.
Unfortunately i guess it's too complicated to do this via css, so the only fast-easy solution is to use javascript to check the thumbnails aspect ratio and change the css inline for certain thumbs:
the script should check the height:width ratio of every thumbnail, if the ratio is lower than 0.6666666666666667 (2:3) then set via css height:100%; max-width:none; to override the actual rules. Can also be added a class, if easier.

How can this be done via javascript? (jquery can be used too as the library is already present for other plugins).

The structure of the grid is like this:

<ul id="gallery" class="gallery-grid">
  <li class="grid-block">
    <div class="block-wrap">
        <div class="link-wrap">
            <a href="<?php echo $photo->sourceImageFilePath; ?>" class="block-link" target="_blank" >
                <img class="block-thumb" src="<?php echo $photo->thumbImageFilePath; ?>"/>              
            </a>
        </div>  
    </div>
  </li>
</ul>

Ofcourse if you can find a viable css answer to my previous question is even more welcome! thanks!

Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Oct 28, 2012 at 23:10 GruberGruber 2,2985 gold badges31 silver badges51 bronze badges 3
  • 1 If you search for "javascript get image height" in google you'll find a lot of results, don't be so lazy – zerkms Commented Oct 28, 2012 at 23:12
  • 1 Search a bit harder. I found a related topic just sitting on the first page of a Google search. – Joseph Commented Oct 28, 2012 at 23:15
  • i guess i should have specified somewhere that i'm fairly ignorant to what comes to javascript programming. Ofcourse i've been googlin around for this question, but i havent find something fairly easy to understand and implement to my own need, my bad, i guess... – Gruber Commented Oct 28, 2012 at 23:20
Add a comment  | 

2 Answers 2

Reset to default 12
var ratio = $img.height() / $img.width();
if ( ratio < (2/3) ) { $img.css({ height: '100%', maxWidth: 'none' }) }

You might just want to query every thumbnail and do the math.

$('img').each(function(_, img) {
    var $this = $(this);

    if( $this.height() / $this.width() < 0.6666666666666667 ) {
        $this.css({
            height: '100%',
            maxWidth: 'none'
        });
    }
});
发布评论

评论列表(0)

  1. 暂无评论