Last active
August 26, 2020 05:24
-
-
Save zitrusblau/47e9dbc58806b32bc6f1e7d869de638e to your computer and use it in GitHub Desktop.
Make fitVids work with lazySizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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