Skip to content

Instantly share code, notes, and snippets.

@firejune
Created August 18, 2016 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firejune/ee246da0d9807d314565749b4d457dca to your computer and use it in GitHub Desktop.
Save firejune/ee246da0d9807d314565749b4d457dca to your computer and use it in GitHub Desktop.
Dynamic background displayer
window.fully = (function(win, doc) {
function viewer(options) {
win._cb = function(obj) {
var image = doc.getElementById(options.elementId);
image.src = '//www.bing.com' + obj.query.results.json.images.url;
if (image.complete) {
addClassName(image, 'active', true);
} else {
image.onload = function() {
addClassName(image, 'active', true);
};
}
}
injectScript('//query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.bing.com%2FHPImageArchive.aspx%3Fformat%3Djs%26idx%3D0%26n%3D1%26mkt%3Den-US%22&format=json&callback=_cb');
}
function player(options) {
var tv;
var video = doc.getElementById(options.elementId);
var screen = video.parentNode;
var videos = options.videos;
win.onresize = vidRescale;
win.onYouTubePlayerAPIReady = function() {
tv = new YT.Player(options.elementId, {
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange
},
playerVars: {
autoplay: 0, autohide: 1, modestbranding: 0, rel: 0, showinfo: 0,
controls: 0, disablekb: 1, enablejsapi: 0, iv_load_policy: 3
}
});
video = screen.childNodes[1];
addEvent(video, 'click', function(event) {
event.stopPropagation();
});
vidRescale();
};
function onPlayerReady() {
tv.loadVideoById({
videoId: videos[Math.floor(Math.random() * (videos.length - 1 + 1))],
suggestedQuality: 'hd720'
});
tv.mute();
}
function onPlayerStateChange(e) {
if (e.data === 1) {
addClassName(video, 'active', true);
} else if (e.data === 0) {
tv.seekTo(0);
}
}
function vidRescale() {
var w = win.innerWidth + 400;
var h = win.innerHeight + 400;
if (w / h > 16 / 9) {
tv.setSize(w, w / 16 * 9);
} else {
tv.setSize(h / 9 * 16, h);
}
video.style.left = (win.innerWidth - video.offsetWidth) / 2 + 'px';
}
injectScript('//www.youtube.com/player_api');
}
function injectScript(c){var a=doc.createElement("script"),b=doc.getElementsByTagName("script")[0];a.async=!0;a.src=c;b.parentNode.insertBefore(a,b)};
function addClassName(a,c,b) {if(a.className) {var e=a.className.split(" ");if(b) {b=c.toUpperCase();for(var d=0;d<e.length;d++)e[d].toUpperCase()==b&&(e.splice(d,1),d--)}e[e.length]=c;a.className=e.join(" ")}else a.className=c}
function addEvent(a,c,b) {"object"!=typeof a&&(a=doc.getElementById(a));a&&(a.attachEvent?(a["e"+c+b]=b,a[c+b]=function() {a["e"+c+b](win.event)},a.attachEvent("on"+c,a[c+b])):a.addEventListener(c,b,!1))};
return function(options) {
(win.matchMedia
&& win.matchMedia('(max-device-width: 960px)').matches
|| win.screen.width <= 960 ? viewer : player)(options);
};
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment