Last active
January 26, 2018 16:50
-
-
Save unnamedfeeling/00f26b4b442a600c85599f213b1d24cb to your computer and use it in GitHub Desktop.
Lazy wistia embed - will load nasty wistia scripts only when needed! + scripts lazyloader
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
<script> | |
// this one might go anywhere in your code | |
// inline it to get best results | |
loadscripts=function(arr){ | |
!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var s,a,c,d=[],i=e.scripts[0],o="onreadystatechange",f="readyState";s=r.shift();)a=e.createElement(t),"async"in i?(a.async=!1,e.head.appendChild(a)):i[f]?(d.push(a),a[o]=n):e.write("<"+t+' src="'+s+'" defer></'+t+">"),a.src=s}(document,"script",arr) | |
}; | |
// loader end | |
// this one is for attaching event to your play button and load scripts | |
$(document).on('click', '.js-wistia-placeholder .playbtn', function(event){ | |
var cont=$(this).parent(); | |
cont.css({'opacity':0.8}); | |
// loadscripts(["//fast.wistia.com/embed/medias/nsn69b29b3.jsonp","//fast.wistia.com/assets/external/E-v1.js"]); | |
loadscripts(["//fast.wistia.com/assets/external/E-v1.js"]); | |
setTimeout(function(){ | |
cont.fadeOut(500, function() { | |
$(this).parent().find('.js-wistia-video').fadeIn(300) | |
}) | |
}, 2000) | |
}); | |
// end attach event | |
// we dont need another request to wistia servers - this one is loaded instead of video json | |
var VIDEO_ID='your video id here'; //you might not need caps in your code | |
window._wq = window._wq || []; | |
_wq.push({ | |
id: VIDEO_ID, | |
options: { | |
videoFoam: true, | |
autoPlay: true | |
} | |
}); | |
// don`t forget to adjust settings for your needs... | |
// end wistia init code | |
</script> | |
<!-- this html is intended to hold video immage not to break everything on your site --> | |
<!-- you may adjust it as you wish --> | |
<div class="wistia_responsive_padding js-wistia-video" style="display:none;padding:56.25% 0 0 0;position:relative;"> | |
<div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;"> | |
<div class="wistia_embed wistia_async_jvfhf0aaie" style="height:100%;width:100%"> </div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment