Skip to content

Instantly share code, notes, and snippets.

@naokazuterada
Last active April 5, 2020 07:00
Show Gist options
  • Save naokazuterada/9a738fdf1160b5385d615ec472880792 to your computer and use it in GitHub Desktop.
Save naokazuterada/9a738fdf1160b5385d615ec472880792 to your computer and use it in GitHub Desktop.
Youtubeなどのiframeをアスペクト比維持しつつリサイズするスクリプト(required jquery)
const resizeIframeWithKeepingAspectRatio = function () {
$('iframe').each(function () {
const me = $(this);
if (/(youtube\.com|vimeo\.com)/.test(me.attr('src'))) {
const height = me.width() * (me.attr('height') / me.attr('width'));
me.css('height', `${height}px`);
}
});
};
$(window).on('resize', resizeIframeWithKeepingAspectRatio);
resizeIframeWithKeepingAspectRatio();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment