Skip to content

Instantly share code, notes, and snippets.

@alfongj
Created February 15, 2012 18:58
Show Gist options
  • Save alfongj/1838160 to your computer and use it in GitHub Desktop.
Save alfongj/1838160 to your computer and use it in GitHub Desktop.
Shorter version of Jeffrey Way's jQuery slider done in episode 15 of Learn jQuery in 30 days
(function($) {
var sliderUL = $('div.slider').css('overflow', 'hidden').children('ul'),
imgs = sliderUL.find('img'),
imgWidth = imgs[0].width, // 600
imgsLen = imgs.length, // 4
current = 0, //0: first
$('#slider-nav').show().find('button').on('click', function() {
var direction = $(this).data('dir'),
move = ( direction === 'next' ) ? 1 : -1; //1 forward, -1 backward
current += move;
current = (current >= 0) ? (current % imgsLen) : (imgsLen-1);
sliderUL.animate({
'margin-left': -current*imgWidth
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment