Skip to content

Instantly share code, notes, and snippets.

@madx
Created November 19, 2008 21:59
Show Gist options
  • Save madx/26755 to your computer and use it in GitHub Desktop.
Save madx/26755 to your computer and use it in GitHub Desktop.
A simple JS slideshow
/*
* js3 - javascript simple slideshow system
*
*/
js3 = {
"current": 0,
"init": function() {
$('.slide').hide();
$('#menu').hide();
$('.incremental > *').hide();
js3.slides = $('#slideshow > div.slide');
slidecount = 0;
js3.slides.each(function() {
/* Append to menu */
title = $(this).find('h1').html();
ref = "javascript:js3.menuJumpTo("+slidecount+")";
entry = "<li><a href=\""+ref+"\" title=\"Jump to slide\">"+title+"</li>"
$('#menu ul').append(entry);
/* Compute footnotes */
fn_num = 1;
$(this).find('.fn').each(function() {
$(this).append('<sup>'+fn_num+'</sup>');
fn_num++;
});
slidecount++;
});
js3.count = slidecount;
js3.curri = 0;
js3.main = $('#layout #contents');
document.onkeypress = js3.keyNav;
js3.showSlide();
},
"currentSlide": function() {
return js3.slides.eq(js3.current);
},
"nthSlide": function(num) {
return js3.slides.eq(num);
},
"showSlide": function() {
slide = js3.currentSlide();
js3.main.html(slide.html());
slide.find('.fn').each(function() {
num = $(this).children('sup').text();
title = $(this).attr('title');
$('#footnotes').append("<p>"+num+": "+title+"</p>");
});
js3.main.addClass(slide.attr('class'));
},
"flushLayout": function() {
js3.main.html('').removeClass();
$('#layout #footnotes').html('');
},
"jumpTo": function(num) {
js3.flushLayout();
js3.current = num;
js3.showSlide();
},
"nextStep": function(force) {
/* Show the next slide or the next item for incremental display */
if(!force && js3.incLen() > 0) {
if(js3.curri < js3.incLen()) {
js3.main.find('.incremental').children().eq(js3.curri).show();
js3.curri++;
} else {
js3.curri = 0;
js3.nextStep(true);
}
} else if(js3.count > js3.current+1) {
js3.flushLayout();
js3.current += 1;
js3.showSlide();
}
},
"prevStep": function() {
if(js3.current > 0) {
js3.flushLayout();
js3.current -= 1;
js3.curri = 0;
js3.showSlide();
}
},
"toggleMenu": function() {
$('#menu').toggle();
},
"menuJumpTo": function(n) {
js3.jumpTo(n);
js3.toggleMenu();
},
"slideTitle": function(num) {
return js3.nthSlide(num).children('h1').eq(0).html();
},
"incLen": function() {
return js3.main.find('.incremental > *').length;
},
"keyNav": function(key) {
key = key || window.event;
switch(key.which) {
case 32: js3.nextStep(); break;
case 8: js3.prevStep(); break;
case 103: js3.toggleMenu(); break;
}
switch(key.keyCode) {
case 39: js3.nextStep(); break;
case 37: js3.prevStep(); break;
}
}
};
$(function() { js3.init(); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment