问题
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
回答1:
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);
}
});
来源:https://stackoverflow.com/questions/3075073/jquery-cycle-plugin-with-different-timeout-values-for-each-slide