Skip to content

Instantly share code, notes, and snippets.

@HoundstoothSTL
Last active October 12, 2020 03:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HoundstoothSTL/5631366 to your computer and use it in GitHub Desktop.
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.
/* --- 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);
});
}());
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);
}
};
<!-- 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