Skip to content

Instantly share code, notes, and snippets.

@pixiebox
Last active February 15, 2016 22:39
Show Gist options
  • Save pixiebox/f84a61c36f1508a86972 to your computer and use it in GitHub Desktop.
Save pixiebox/f84a61c36f1508a86972 to your computer and use it in GitHub Desktop.
<a class="vimeo-lazy-link" data-height="349" data-width="620" data-video="3137949" href="http://vimeo.com/3137949"></a>
function fetchJSONFile(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
(function() {
// Find all Vimeo videos
var lazy_links = document.querySelectorAll('.vimeo-lazy-link')
, vimeo_id;
for (var i = 0; i < lazy_links.length; i++) {
vimeo_id = lazy_links[i].getAttribute('data-video');
fetchJSONFile('http://vimeo.com/api/v2/video/' + vimeo_id + '.json?callback=?', function(response){
lazy_links[i].setAttribute('style', 'background: url(' + response[0].thumbnail_medium + '); background-size: cover; width: 600px; height: 350px;');
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment