Skip to content

Instantly share code, notes, and snippets.

@modcab
Last active March 10, 2016 12:58
Show Gist options
  • Save modcab/fd39b0c8e4e944879fcc to your computer and use it in GitHub Desktop.
Save modcab/fd39b0c8e4e944879fcc to your computer and use it in GitHub Desktop.
jQuery - Make video in iframe responsive
// Makes iframes inside of a node body responsive.
Drupal.behaviors.responsive_iframes = {
attach: function (context) {
$(document).ready(function() {
// Iframe needs to have the class .responsive.
$('.field-name-body iframe').each(function(index, value) {
// Height and width both need int values.
var height = parseInt($(this).attr('height')) || null;
var width = parseInt($(this).attr('width')) || null;
if ((height !== null) && (width !== null)) {
var ratio = (height/width) * 100;
// Adding a wrapper around the iframe with the ratio in the iframe.
$(this).wrap("<div style=\"position: relative;padding-bottom:" + ratio + "%\"></div>");
// Modifying css for the iframe so it's responsive.
$(this).css({
position: 'absolute',
top: 0,
left: 0,
width: "100%",
height: "100%"
});
}
});
});
}
}
})(jQuery, Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment