Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
propiedad jQuery min/max de una matriz de elementos

¿Existe una forma sencilla de encontrar la propiedad min/max de una matriz de elementos en jQuery?

Constantemente me encuentro cambiando dinámicamente el tamaño de grupos de elementos en función de las contrapartes mínimas y máximas. La mayoría de las veces esto se refiere al ancho y/o alto de un elemento, pero estoy seguro de que esto podría aplicarse a cualquier propiedad de un elemento.

Yo suelo hacer algo como esto:

 var maxWidth = 0; $('img').each(function(index){ if ($(this).width() > maxWidth) { maxWidth = $(this).width(); } });

Pero parece que deberías poder hacer algo como esto:

 var maxWidth = $('img').max('width');

¿Existe esta funcionalidad en jQuery o alguien puede explicar cómo crear un complemento básico que haga esto?

¡Gracias!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Usar Fast JavaScript Max/Min - John Resig

Ejemplo con tres logos de google, yahoo y bing.

HTML

 <img src="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png" alt="Google Logo" /><br/> <img src="http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png" alt="Yahoo Logo" /><br/> <img src="http://www.bing.com/fd/s/a/h1.png" alt="Bing Logo" />

JavaScript

 $(document).ready(function(){ // Function to get the Max value in Array Array.max = function( array ){ return Math.max.apply( Math, array ); }; // Function to get the Min value in Array Array.min = function( array ){ return Math.min.apply( Math, array ); }; //updated as per Sime Vidas comment. var widths= $('img').map(function() { return $(this).width(); }).get(); alert("Max Width: " + Array.max(widths)); alert("Min Width: " + Array.min(widths)); });

PD: jsfiddle aquí

over 4 years ago · Santiago Trujillo Report

0

Puede usar apply fuera del contexto de OO, no es necesario extender el prototipo:

 var maxHeight = Math.max.apply( null, $('img').map(function(){ return $(this).height(); }).get() );
over 4 years ago · Santiago Trujillo Report

0

Me gusta la solución elegante publicada como un ejemplo de .map() en los documentos de jQuery sobre cómo igualar las alturas de div . Básicamente lo adapté para trabajar con anchos e hice una demostración .

 $.fn.limitWidth = function(max){ var limit = (max) ? 'max' : 'min'; return this.width( Math[limit].apply(this, $(this).map(function(i,e){ return $(e).width(); }).get() ) ); }; // Use the function above as follows $('.max-width').limitWidth(true); // true flag means set to max $('.min-width').limitWidth(); // no flag/false flag means set to min
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!