Last active
October 12, 2020 03:29
-
-
Save HoundstoothSTL/5631366 to your computer and use it in GitHub Desktop.
On-demand vimeo embedded videos with oEmbed API, no jQuery, just javascript. Videos are not loaded on page load for performance reasons, use placeholder images, then on click, fire off the javascript.
This file contains hidden or 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
/* --- Just your general app file --*/ | |
(function(){ | |
var triggers = document.querySelectorAll('[data-js="embedTrigger"]'); | |
[].forEach.call( triggers, function(element) { | |
element.addEventListener('click', function() { | |
Embed.init({ | |
elementId: this.id, | |
videoId : this.getAttribute('data-video'), | |
autoplay : true, | |
vidWidth: 640, | |
vidHeight: 400 | |
}); | |
}, false); | |
}); | |
}()); |
This file contains hidden or 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
var Embed = { | |
config : { | |
elementId : 'embed', | |
trigger: document.querySelectorAll('[data-js="embedTrigger"]'), | |
videoId : 7100569, | |
autoplay : true, | |
vidWidth: 640, | |
vidHeight: 400 | |
}, | |
trigger : function() { | |
// Move trigger here if wanted | |
}, | |
put : function(video) { | |
var el = document.getElementById(this.config.elementId); | |
el.innerHTML = unescape(video.html); | |
}, | |
init : function(options) { | |
if( typeof options === "object" ) { | |
this.config = options; | |
} | |
var js = document.createElement('script'), | |
callback = 'Embed.put', | |
endpoint = 'http://www.vimeo.com/api/oembed.json', | |
videoUrl = encodeURIComponent("http://www.vimeo.com/" + this.config.videoId), | |
w = this.config.vidWidth, | |
h = this.config.vidHeight, | |
autoplay = this.config.autoplay, | |
url = endpoint | |
+ '?url=' + videoUrl | |
+ '&width=' + w | |
+ '&autoplay=' + autoplay | |
+ '&callback=' + callback; | |
js.setAttribute('type', 'text/javascript'); | |
js.setAttribute('src', url); | |
js.setAttribute('async', true); | |
document.getElementsByTagName('head').item(0).appendChild(js); | |
} | |
}; |
This file contains hidden or 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
<!-- usage in HTML --> | |
<div id="testVideo2" data-video="58834399" data-js="embedTrigger"> | |
<img src="http://placehold.it/640x400"> <!-- Uses placeholder image --> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment