jQuery cycle plugin with different timeout values for each slide

走远了吗. 提交于 2019-12-24 08:28:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!