Skip to content

Instantly share code, notes, and snippets.

@eeroan
Created April 26, 2013 07:37
Show Gist options
  • Save eeroan/5465559 to your computer and use it in GitHub Desktop.
Save eeroan/5465559 to your computer and use it in GitHub Desktop.
Slideshow from directory index
((function(){
if(typeof window.jQuery == 'undefined') {
var script = document.createElement('script');
script.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
document.getElementsByTagName('head')[0].appendChild(script);
var attempts = 15;
;(function(){
if(typeof window.jQuery == 'undefined') {
if(--attempts > 0) {
window.setTimeout(arguments.callee, 250);
}
} else {
init();
}
})();
}
function init() {
j = jQuery;
var links = j('a[href$=".gif"], a[href$=".jpg"], a[href$=".png"]');
var image_list = links.map(function() {return j(this).attr('href')});
var current = 0;
var last = image_list.length-1;
var container = j('<div class="container"></div>');
j('body').empty().append(container);
j(document).keyup(function(e) {
if(e.keyCode == 37) prev();
else if(e.keyCode == 39) next();
});
set_image(0);
function prev() {
if(current==0) current = last;
else current--;
set_image(current);
}
function next() {
if(current==last) current=0;
else current++;
set_image(current);
}
function set_image(i) {
j('.container').empty().append('<img src="'+image_list[i]+'"/>');
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment