我有一个每 2400 毫秒触发一次“抽动”的时钟。在“tic”上,我希望我的父 div 的子级切换类,但不是同时切换:我使用它们父级的索引来及时偏移触发器,以便它以某种波的形式发生。最重要的是,或者我应该从它开始,我不希望在用户滚动时发生这些。我设法让我的容器停止使用 .off() 收听“tic”,但我似乎无法清除导致 offset 的超时。
这是一个 jsfiddle:https ://jsfiddle.net/andinse/w878ft4z/7/
我是 jQuery 新手,在这一切的嵌套中有点迷失。
非常感谢任何帮助!
var $parents = $('.parents');
var $parent = $('.parent');
var $child = $('.child');
var $speed = 2400;
var $loopLength = 5;
setInterval(function clock() {
$parents.trigger('tic');
}, $speed);
$.fn.play = function() {
$(this).on('tic', function() {
$child.each(function() {
var $that = $(this);
var $offset = ($that.parent().index() % $loopLength) * $speed / $loopLength;
setTimeout(function() {
$that.toggleClass('special');
}, $offset);
});
return this;
});
};
$parents.play();
$(document).scroll(function() {
$parents.off('tic');
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
$parents.play();
}, 200));
});