Created
June 17, 2011 13:15
-
-
Save iamntz/1031395 to your computer and use it in GitHub Desktop.
ntz infinite carousel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ){ | |
$.fn.ntz_infiniteCarousel = function() { | |
return this.each(function(){ | |
var carousel = $(this), | |
etalon = $('li:first', carousel); | |
carousel.wrap('<div class="ntz_infiniteCarouselWrap"></div>'); | |
var w = carousel.closest('.ntz_infiniteCarouselWrap'), //the wrap | |
n = $('<a href="#" class="nav prev"/><a href="#" class="nav next"/>'), // the navigation | |
inc = etalon.outerWidth(true), // increment - the width of an element ( is the same amount of px to be scrolled ) | |
ul = $('ul', carousel), | |
perPage = Math.ceil( w.width()/inc ), // how many elements are displayed per page | |
fX = $('li:lt(' + perPage + ')', ul).clone(), // first and last X elements ( x = perPage) | |
lX = $('li:gt('+ ( $('li', ul).length - perPage-1 ) +')', ul).clone(), | |
s = $('li.s', carousel); // selected | |
w.addClass( carousel.attr('id') ); | |
ul.append(fX).prepend(lX); // adding elemnts on begining and the end for infinite loop | |
var ulW = $('li', ul).length * inc, // width for ul | |
maxPos = -1*(ulW - inc*perPage - perPage); // max position | |
ul.width( ulW ).css({marginLeft:-inc*perPage}); | |
if( s.length ){ | |
ul.css({ marginLeft:-s.index()*inc + inc}); | |
} | |
n.click(function(){ | |
var t = $(this), | |
curPos = parseInt(ul.css('marginLeft'), 10); | |
if( ul.is(':animated') ) {return false;} // we don't need multiple scrolls | |
if( t.hasClass('prev') ){ // scroll left | |
if( curPos >= 0 ){ | |
ul.css({ marginLeft: -( ulW - inc * perPage*2 ) }); // infinite loop | |
} | |
ul.animate({ marginLeft:'+='+inc }) | |
} | |
if ( t.hasClass('next') ) { //scroll right | |
if( maxPos >= curPos ){ | |
ul.css({ marginLeft: - inc * perPage }); // infinite loop | |
} | |
ul.animate({ marginLeft:'-='+inc }); | |
} | |
return false; | |
}); | |
n.appendTo(w); | |
}); | |
}; // ntz_infiniteCarousel | |
})( jQuery ); | |
$('#infinity').ntz_infiniteCarousel(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment