Skip to content

Instantly share code, notes, and snippets.

@jefflembeck
Forked from dangayle/gist:2040573
Created March 15, 2012 00:33
Show Gist options
  • Save jefflembeck/2040712 to your computer and use it in GitHub Desktop.
Save jefflembeck/2040712 to your computer and use it in GitHub Desktop.
jquery slider loop thing
//Originally from http://trendmedia.com/news/infinite-rotating-images-using-jquery-javascript/
var InfiniteRotator =
{
itemInterval: 3000,
infiniteLoop: function() {
setInterval(function(){
$('.sliderItem').eq(currentItem).stop().transition({opacity: 0},2000);
//if at last item, reset currentItem to 0
if(currentItem == numberOfItems -1){
currentItem = 0;
}else{
currentItem++;
}
$('.sliderItem').eq(currentItem).stop().transition({opacity: 1},1000);
}, InfiniteRoator.itemInterval);
},
init: function()
{
//set values
var initialFadeIn = 1000,
fadeTime = 2500,
numberOfItems = $('.sliderItem').length,
currentItem = 0,
infiniteLoop;
//fade in initial slide
$('.sliderItem').eq(currentItem).stop().transition({opacity: 1},2000);
//start looping through slides
//interrupt loop when hovering over navigation
$('#sliderNavigation li a').hover(
function () {
currentItem = $(this).closest('li').index();
clearInterval(infiniteLoop);
$('.sliderItem').stop().transition({opacity: 0},2000);
$('.sliderItem').eq(currentItem).stop().transition({opacity: 1},1000);
},InfiniteRotator.infiniteLoop());
}
};
InfiniteRotator.init();
@jefflembeck
Copy link
Author

Without being able to test, and without the rest of this, this is a quick start to show how to move into obj literal syntax.

@dangayle
Copy link

There is a typo on line 20, and a console warning that "currentItem is not defined" on line 7

Thanks!

@jefflembeck
Copy link
Author

Oh yeah, this isn't a working version of this. I was hoping to make a couple of changes and maybe you'd see how this stuff can start be moved.

@jefflembeck
Copy link
Author

Without actually knowing what's going on with this/ what you're trying to solve/ what the html looks like so it can be tested, I can't really go beyond that.

That being said, currentItem isn't defined because it's not in scope there, bring it up into the syntax with itemInterval, and then reference it from there.

And there's a typo on line 20. Pretty easy to fix

@dangayle
Copy link

I appreciate your help on this. There's a working version of the old code here: http://knocktastic.com/clients/unleashed/slider/

I'd really like to get it so that the hover/pause event can be a callback so that I can reference it outside of this code. Ultimately, I want to turn it into a jquery plugin, but I'm more interested in learning how the object literal syntax works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment