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

291
Views
¿Cómo volver a calcular la altura máxima de los elementos al cambiar el tamaño?

Me enfrento a un problema, donde he creado acordeones/elementos de preguntas frecuentes que puede alternar (abrir y cerrar) y cambiar a otro y cerrar el anterior que funciona bien. Pero el problema aquí es que si cambio el tamaño de mi navegador o cambio el modo de orientación a paisaje o retrato, la altura máxima no se vuelve a calcular.

Creé una función "accordionHeight" que funciona en Click pero no puedo pasar el "elemento" a esta función para volver a calcular fuera de mi evento de clic.

Porque la altura obviamente solo cambiará al hacer clic. Pero quiero que se vuelva a calcular al girar el "elemento__box" abierto actual, pero está fuera del alcance.

Tengo un ejemplo real en Codepen en lugar de código.

Mi JS:

 (() => { const accordion = document.querySelector('[bke-data="accordion"]'); if (!accordion) { return; } const items = accordion.querySelectorAll('.accordion__item'); const accordionHeight = (element, elementHeight) => { element.style.setProperty(`--max-height`, `${elementHeight}px`); }; const toggleAccordion = (item) => { const itemBox = item.querySelector('.accordion__box'); const itemBoxHeight = itemBox.scrollHeight; const openItems = accordion.querySelectorAll('.open'); const itemsLength = openItems.length; const currentItem = item; // itemBox.style.setProperty(`--max-height`, `${itemBoxHeight}px`); accordionHeight(itemBox, itemBoxHeight); if (itemsLength) { openItems.forEach((item) => { item !== currentItem && item.classList.remove('open'); }); } item.classList.toggle('open'); }; items.forEach((item) => { item.addEventListener('click', () => toggleAccordion(item)); }); window.addEventListener('resize', accordionHeight); })();

Codepen código completo + ejemplo: https://codepen.io/Merdzanovich/pen/MWvaryG

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Por favor, vea el siguiente ejemplo de manejo de eventos de cambio de tamaño. Nota: Ejecute el fragmento en modo de página completa

 <!DOCTYPE html> <html> <body onresize="myFunction()"> <p>Try to resize the browser window to display the windows height and width.</p> <p id="demo"></p> <p><strong>Note:</strong> this example will not work properly in IE8 and earlier. IE8 and earlier do not support the outerWidth/outerHeight propery of the window object.</p> <script> function myFunction() { var w = window.outerWidth; var h = window.outerHeight; var txt = "Window size: width=" + w + ", height=" + h; document.getElementById("demo").innerHTML = txt; } </script> </body> </html>

about 4 years ago · Juan Pablo Isaza 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!