我在 stackoverflow 问题中发现了抖动效果(这里)
代码如下所示;
jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
this.each(function() {
$(this).css("position","relative");
for (var x=1; x<=intShakes; x++) {
$(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
.animate({left:intDistance}, ((intDuration/intShakes)/2))
.animate({left:0}, (((intDuration/intShakes)/4)));
}
});
return this;
};
但我需要一种方法来添加回调函数(或任何其他简单的方法),以便在效果之前改变抖动元素的边框颜色,并在动画完成后切换到原始颜色。
我尝试如下但没有机会(边框立即变为原始颜色)
jQuery.fn.shake = function(intShakes, intDistance, intDuration,callback) {
this.each(function() {
$(this).css("position","relative");
for (var x=1; x<=intShakes; x++) {
$(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
.animate({left:intDistance}, ((intDuration/intShakes)/2))
.animate({left:0}, (((intDuration/intShakes)/4)));
}
});
if(callback) callback();
return this;
};
像这样打电话
$elem.css('borderColor','red');
$elem.shake(3,5,500,function(){
$elem.css('borderColor','#a6caf0');})
您可以在此处找到一个 JSFiddle 示例。(如果您在小提琴中取消回调函数,您会看到边框正确变为红色但回调失败。)
现在谢谢...