geo-coop/web/themes/custom/geofresco/dist/js/responsive-iframes.js

36 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2019-08-31 08:41:51 -04:00
// From https://benmarshall.me/responsive-iframes/
// Note that the JavaScript approach does not combine with his recommended CSS
// approach, and it would probably be better/smoother to combine them.
// @TODO Or, best, write a text formatter that does the below and assigns the
// appropriate class, for example .embed-responsive-4by3
2019-08-31 09:30:34 -04:00
(function ($) {
'use strict';
2019-08-31 09:34:27 -04:00
// Find all iframes
2024-10-03 14:02:15 -07:00
var $iframes = $( "iframe" ).not(".mastodon-embed");
2019-08-31 08:41:51 -04:00
2019-08-31 09:34:27 -04:00
// Find and save the aspect ratio for all iframes
$iframes.each(function () {
$( this ).data( "ratio", this.height / this.width )
2020-02-05 14:16:04 -05:00
// Remove the hardcoded width and height attributes
2019-08-31 09:34:27 -04:00
.removeAttr( "width" )
.removeAttr( "height" );
2019-08-31 08:41:51 -04:00
});
2019-08-31 09:34:27 -04:00
// Resize the iframes when the window is resized
$( window ).resize( function () {
$iframes.each( function() {
2020-02-05 14:16:04 -05:00
// Get the parent container's width
2019-08-31 09:34:27 -04:00
var width = $( this ).parent().width();
$( this ).width( width )
.height( width * $( this ).data( "ratio" ) );
});
// Resize to fix all iframes on page load.
}).resize();
2019-08-31 09:30:34 -04:00
})(jQuery);
2019-08-31 09:34:27 -04:00
// Note that we know we have JQuery because our geofresco/global relies on bulma/global
// which pulls in JQuery already.