简体   繁体   中英

Equal height to width, single images

How can I add class "equal" if an image that is equal in width and height.

Here is my code:

var img = $('img', this);
var width = img.width();
var height = img.height();

if(width == height){
$(this).img.addClass('equal');
}

This bit:

if(width == height){
    $(this).img.addClass('equal');
}

should be:

if(width == height){
    img.addClass('equal');
}

as you already have your img as a jQuery object (by doing var img = $('img', this); )

  1. Get all images
  2. Iterate and add the class.

     //get all images let $images = $('img'); // add classes $.each($images, function(key, img) { if ( img.height === img.width ) { $(img).addClass('equal'); } }); 

Example: https://jsfiddle.net/6ot4ks87/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM