views:

44

answers:

1

I'm trying to use the jQuery cycle plugin to cycle round different quotes. I would like to have the quotes displayed for different amount of time depending on the length of the quote. To achieve this I get the content mangagement system to output the amount of seconds as a class name such as dur13 would be for 13 seconds.

This is my non-working attempt:

$('.featureFade').cycle({cycleTimeout: 10, after: onCycleAfter});

function onCycleAfter() { 
    $('.featureFade').cycle('pause');
    var duration = $(this).attr('class').substring($(this).attr('class').indexOf('dur')+3)
    setTimeout(oncycleEnd, duration * 1000); 
}
function oncycleEnd() { 
    $('.featureFade').cycle('resume');
}

Is this possible with cycle? If not is there another plugin that would work? I don't really need fancy effects, just fade in fade out would be enough.

Many thanks

+2  A: 

There's a timeoutFn option you can use, like this:

$('.featureFade').cycle({
  timeoutFn: function(currElement, nextElement, opts, isForward) { 
    var duration = $(currElement).attr('class').substring($(currElement).attr('class').indexOf('dur')+3)
    return duration * 1000;
  }
});

However, instead of a class you can use a data attribute, something like this:

<img data-duration="2000" src="..." />

Then your code is a bit simpler:

$('.featureFade').cycle({
  timeoutFn: function(currElement, nextElement, opts, isForward) { 
    return parseInt($(currElement).attr('data-duration'), 10);
  }
});
Nick Craver
I love the data attribute idea, much cleaner.
Max
@Max - Try alerting the number, make sure that your duration is coming out like you think it is....for example if you had `class="dur2 otherClass"` you'd get a isNaN exception...that's why I suggest the data attribute route.
Nick Craver
This worked perfectly, thanks!
Max