【发布时间】:2011-06-22 08:05:00
【问题描述】:
switch (options.effect) {
case 'h-blinds-fadein':
$('.child').each(function(i) {
$(this).stop().css({
opacity: 0
}).delay(100 * i).animate({
'opacity': 1
}, {
duration: options.speed,
complete: (i !== r * c - 1) ||
function() {
$(this).parent().replaceWith(prev);
options.cp.bind('click', {
effect: options.effect
}, options.ch);
}
});
});
break;
case 'h-blinds-fadein-reverse':
$('.child').each(function(i) {
$(this).stop().css({
opacity: 0
}).delay(100 * (r * c - i)).animate({
'opacity': 1
}, {
duration: options.speed,
complete: (i !== 0) ||
function() {
$(this).parent().replaceWith(prev);
options.cp.bind('click', {
effect: options.effect
}, options.ch);
}
});
});
break;
....more cases
}
我还有很多类似的案例。我能想到的一种方法是编写函数?我不确定我对这门语言还是很陌生
对不起,i 是 each() 函数的索引,它是 $('.child') 的大小,而 r 和 c 只是包含 '.孩子'。 r 和 c 可以是任意数字,例如r=5 c=5
【问题讨论】:
-
@JeffAtwood 至少将其移至 codereview.SE
标签: javascript jquery refactoring case switch-statement