Skip to content

Instantly share code, notes, and snippets.

@zitrusblau
Last active August 26, 2020 05:24
Show Gist options
  • Save zitrusblau/47e9dbc58806b32bc6f1e7d869de638e to your computer and use it in GitHub Desktop.
Save zitrusblau/47e9dbc58806b32bc6f1e7d869de638e to your computer and use it in GitHub Desktop.
Make fitVids work with lazySizes
<div class="fluid-embed">
<iframe frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="" class="lazyload" data-src="https://www.youtube.com/embed/4rfnJPhgd9E?feature=oembed"></iframe>
</div>
(function($) {
$(document).on( 'lazyloaded', function(e){
// trigger fitVids on lazyloaded iframes
if( e.target.localName === 'iframe' && typeof $.prototype.fitVids === 'function' ) {
$target_container = $(e.target).parent();
if( $target_container.hasClass('fluid-embed') ) {
// in order to make fitVids kick in, we first need to re-set the src-attribute... duh!
$(e.target).attr('src', $(e.target).attr('data-src'));
$(e.target).removeAttr('data-src');
$target_container.fitVids();
}
}
});
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment