Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * jQuery Carousel
- *
- * By Kieran Marshall
- */
- function Carousel()
- {
- /**
- * The carousel element.
- *
- */
- this.carousel = null;
- /**
- * The button to scroll the carousel to the left
- *
- */
- this.leftButton = null;
- /**
- * The button to scroll the carousel to the right
- *
- */
- this.rightButton = null;
- /**
- * All of the items in the carousel
- *
- */
- this.items = [];
- /**
- * The width of each item
- *
- */
- this.itemWidth = 0;
- /**
- * The number of items that should be visible at any given time.
- *
- */
- this.itemsVisible = 0;
- /**
- * The index of the first visible item.
- *
- */
- this.itemIndex = 0;
- /**
- * The page buttons for the carousel
- *
- */
- this.buttons = [];
- this.hasInterval = false;
- this.interval = null;
- /**
- * Initiate the carousel
- *
- * @return null
- */
- this.init = function (element, leftButton, rightButton)
- {
- this.carousel = $(element);
- this.items = this.carousel.children('li');
- this.itemWidth = this.items.eq(0).outerWidth(false);
- this.leftButton = (leftButton == null) ? null
- : $(leftButton)
- ;
- this.rightButton = (rightButton == null) ? null
- : $(rightButton)
- ;
- var obj = this;
- // If we've been given a left button, assign the click function.
- if(this.leftButton != null)
- this.leftButton.click( function(e) { obj.slideLeft(); e.preventDefault(); });
- // If we've been given a right button, assign the click function.
- if(this.rightButton != null)
- this.rightButton.click( function(e) { obj.slideRight(); e.preventDefault(); });
- // Add an event handler to the resizing of the window to fix the carousel
- $(window).resize(function() {
- obj.adapt();
- });
- // Adapt the carousel so that it fits the right amount of items on the
- // screen.
- this.adapt();
- }
- /**
- * Slide the carousel to the left.
- *
- * @return null
- */
- this.slideLeft = function ()
- {
- // If the carousel is already at the first item
- this.itemIndex = (this.itemIndex == 0) ? this.items.length - this.itemsVisible
- : this.itemIndex - this.itemsVisible;
- this.itemIndex = (this.itemIndex < 0) ? 0
- : this.itemIndex;
- // Animate the slider
- this.animate();
- }
- /**
- * Slide the carousel to the right
- *
- * @return null
- */
- this.slideRight = function ()
- {
- // If the carousel is already at the last item
- this.itemIndex = (this.itemIndex == this.items.length - this.itemsVisible) ? 0
- : this.itemIndex + this.itemsVisible;
- // If the index is greater than the the number of visible items, put it to the
- // right amount.
- this.itemIndex = (this.itemIndex > (this.items.length - this.itemsVisible)) ? this.items.length - this.itemsVisible
- : this.itemIndex;
- // Animate the slider
- this.animate();
- }
- /**
- * Animate the slider to the newly set index
- *
- * @return null
- */
- this.animate = function ()
- {
- this.carousel.animate({
- left: 0 - (this.itemIndex * this.itemWidth)
- } , {
- duration: 250,
- queue: false
- });
- // Update the buttons (if we have any)
- if(this.buttons.length > 0)
- {
- this.buttons.each(function() { $(this).removeClass('current'); });
- this.buttons.eq(this.itemIndex).addClass('current');
- }
- }
- /**
- * Fix the slider when the carousel adapts to a given width
- *
- * @return null
- */
- this.fix = function ()
- {
- //console.log(this.itemIndex + " " + this.items.length+ " " + this.itemsVisible);
- this.itemIndex = (this.itemIndex > (this.items.length - this.itemsVisible)) ? this.items.length - this.itemsVisible
- : 0;
- var maxLocation = this.itemIndex * this.itemWidth;
- this.carousel.css('left', 0 - maxLocation);
- }
- /**
- * Adapt the carousel on resize
- *
- */
- this.adapt = function ()
- {
- this.itemWidth = this.items.eq(0).outerWidth();
- var maxWidth = this.carousel.parent().parent().width();
- maxWidth = (this.leftButton == null) ? maxWidth
- : maxWidth - this.leftButton.width();
- maxWidth = (this.rightButton == null) ? maxWidth
- : maxWidth - this.rightButton.width();
- this.itemsVisible = Math.floor(maxWidth / this.itemWidth);
- this.itemsVisible = (this.itemsVisible == 0) ? 1
- : this.itemsVisible
- ;
- var newWidth = this.itemsVisible * this.itemWidth;
- newWidth = (newWidth == 0) ? this.itemWidth
- : newWidth;
- this.carousel.parent().css('width', newWidth);
- this.fix();
- }
- /**
- * Add page buttons to the carousel
- *
- * @return null
- */
- this.setButtons = function (elem)
- {
- var obj = this;
- this.buttons = $(elem).find('a');
- this.buttons.each( function() {
- $(this).click( function(e) {
- $(obj.buttons).each(function() { $(this).removeClass('current') });
- $(this).addClass('current');
- obj.itemIndex = $(this).parent().index();
- obj.animate();
- e.preventDefault();
- });
- });
- // Set the first item to have the "current" class
- this.buttons.eq(0).addClass('current');
- }
- /**
- * Set an interval for the carousel to automatically move on
- *
- * @return null
- */
- this.addInterval = function ( ms )
- {
- var obj = this;
- this.hasInterval = true;
- this.carousel.hover( function() {
- clearInterval(obj.interval);
- } , function() {
- if(this.hasInterval) {
- obj.doInterval( ms );
- }
- });
- this.doInterval( ms );
- }
- this.doInterval = function( ms )
- {
- var obj = this;
- this.interval = window.setInterval( function() {
- obj.slideRight();
- }, ms);
- }
- this.stopInterval = function() {
- this.hasInterval = false;
- clearInterval(this.interval);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement