向自定义 JQuery 动画添加回调功能的真正方法是什么?
我在 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 示例。(如果您取消回调功能在小提琴中,您会看到边框正确变为红色,但回调失败。)
幸亏现在...
i've found a shake effect in stackoverflow question (here)
Code is like below;
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;
};
but i need a way to add a callback function (or any other simple way) for chaging border color of shaking element before the effect and toggle to orginal color after animation complate.
I tried like below but no chance (border is turning original color immediately)
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;
};
and call like this
$elem.css('borderColor','red');
$elem.shake(3,5,500,function(){
$elem.css('borderColor','#a6caf0');})
You can find a JSFiddle Example here.(If you cancel callback function in fiddle you'll see that borders become red correctly but callback fails.)
Thaks right now...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你:
然后...
现场演示: http://jsfiddle.net /f96ff/8/
不需要计时器。您只需通过(中性)
.show()
方法将callback
函数添加到效果队列即可。这可确保仅在所有动画(来自队列)完成后调用回调
。此外,我推荐一个用于“摇动”状态的 CSS 类:
另外,请注意,我显着重构了
$.fn.shake
的代码。我建议您花几分钟时间来分析我如何改进代码。Here you go:
And then...
Live demo: http://jsfiddle.net/f96ff/8/
A timer is not needed. You just add the
callback
function to the effects queue via the (neutral).show()
method. This ensures that thecallback
is invoked only after all the animations (from the queue) have finished.Additionally, I recommend a CSS class for the "shaking" state:
Also, notice that I significantly refactored the code for
$.fn.shake
. I recommend you to take a few minutes of your time to analyze how I improved the code.你必须设置一个超时,你不能直接调用回调,因为jquery的animate函数是异步的。所以它会直接执行回调。可以做的就是为整个动画的时间设置一个超时。
您也不能只使用 jQuery 的 animate 函数的回调,因为您正在使用多个。
解决方案: http://jsfiddle.net/f96ff/2/
You have to set a timeout, you cant just call the callback directly because the animate function of jquery is async. So it will execute the callback directly. What can be done is set a timeout to the time of the total animation.
You also can't just use the callback of the animate function of jQuery because you are using multiple.
Solution: http://jsfiddle.net/f96ff/2/