(function($) { $.fn.slider = function(options) { //set default options var defaults = { slideSpeed: 1000, timer: 6000, continuous: true, width: 354, height: 271 }; //marge all options into config variable var config = $.extend(defaults, options); //iterate through each item this.each(function() { var $this = $(this); //wrap ul item in a div to hide overflow $this.wrap("
"); //grab all containing divs (in case there are multiples on the page) //and set css styling for them (eliminates need for css stylesheet) $(".sliderWrapper"). css({ height: config.height + "px", width: config.width + "px", overflow: "hidden", 'margin':'0', 'padding':'0' }); //set css styling for ul items $this.css({ 'width': "99999px", 'position': "relative", 'list-style': 'none', 'margin':'0', 'padding':'0' }); //set css styling for child li items $this.children("li").css({ 'float': 'left' }); //create timer to slide items var timer = window.setInterval(function() { //continuous sliding (no return to front slide) if (config.continuous) { //animate sliding to the left $this.animate({ left: "-" + config.width + "px" }, config.slideSpeed, function() { //this is the callback function //remove the first child item and append it to the end $this.css({ left: "0px" }).children(":first").remove().appendTo($this); }); } //non-continuous slider //when you reach the end, slide back to the front else { //get the absolute value of the current position var currentPosition = Math.abs(parseInt($this.css('left'))); //divide it by the content width to determine the current index //add one to get find the index of the next item var nextIndex = (currentPosition / config.width)+1; //check to see if the next child exists if ($this.children().eq(nextIndex).length) { //if another list item exists //determine the position required to show the next element var nextPosition = nextIndex * config.width; //animate sliding to the next element $this.animate({ left: "-" + nextPosition + "px" }, config.slideSpeed); } else { //this is the last list item //animate sliding back to the beginning of the list $this.animate({ left: "0px" }, config.slideSpeed); } } }, config.timer); //end timer }); //end this.each return this; }; //end function })(jQuery);