Can I use jQuery to automatically find and set the width and height of multiple images?

I use Masonry Plugin and to work with images correctly you need to set the width and height in the image tag. All heights of the image will change, but the width will be set to 200 pixels.

Is there a way for jQuery to detect the height of each image and set its height? Thus, I do not need to set the height of each image.

It makes sense?

Hope someone can help! I am new to jQuery.

+2
source share
1 answer

Yes, you can use custom jquery selectors as follows:

//Add custom selector that returns all objects taller than 200px //See http://www.bennadel.com/blog/1457-How-To-Build-A-Custom-jQuery-Selector.htm $.extend($.expr[':'], { over200pixels: function(a) { return $(a).height() > 200; } }); // Seclect all images taller than 200px // Set height to 200px using .css() method // See http://api.jquery.com/css/ $('img:over200pixels').css("height","200px"); 

A loan for which a loan must be granted: jQuery Tips and Tricks

Online demo: http://jsfiddle.net/hvq5m/

+2
source

Source: https://habr.com/ru/post/1342930/


All Articles