Bez popisu

responsive-embeds.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * File responsive-embeds.js.
  3. *
  4. * Make embeds responsive so they don't overflow their container.
  5. */
  6. /**
  7. * Add max-width & max-height to <iframe> elements, depending on their width & height props.
  8. *
  9. * @since Twenty Twenty-One 1.0
  10. *
  11. * @return {void}
  12. */
  13. function twentytwentyoneResponsiveEmbeds() {
  14. var proportion, parentWidth;
  15. // Loop iframe elements.
  16. document.querySelectorAll( 'iframe' ).forEach( function( iframe ) {
  17. // Only continue if the iframe has a width & height defined.
  18. if ( iframe.width && iframe.height ) {
  19. // Calculate the proportion/ratio based on the width & height.
  20. proportion = parseFloat( iframe.width ) / parseFloat( iframe.height );
  21. // Get the parent element's width.
  22. parentWidth = parseFloat( window.getComputedStyle( iframe.parentElement, null ).width.replace( 'px', '' ) );
  23. // Set the max-width & height.
  24. iframe.style.maxWidth = '100%';
  25. iframe.style.maxHeight = Math.round( parentWidth / proportion ).toString() + 'px';
  26. }
  27. } );
  28. }
  29. // Run on initial load.
  30. twentytwentyoneResponsiveEmbeds();
  31. // Run on resize.
  32. window.onresize = twentytwentyoneResponsiveEmbeds;