[英]Javascript/jQuery: start “blinking” on first AJAX call success, stop on next
I'm trying to achieve a "blinking" effect as to signal to the user that a background task is occurring: the button hovers/blinks while the Ajax call is being made, and comes back to full opacity when it's done. 我正在尝试实现一种“闪烁”效果,以向用户发出信号,表明正在执行后台任务:在进行Ajax调用时,该按钮会悬停/闪烁,并在完成后恢复为完全不透明。 However, I'm getting weird and inconsistent results.
但是,我得到的结果很奇怪且不一致。 The closest I got is this:
我最接近的是:
// globals are bad, I know
should_blink = true
// right before Ajax call
should_blink('#my-button', do_animate);
// inside Ajax call success
do_animate = false;
should_blink('#my-button', do_animate);
function should_blink(selector, do_animate) {
console.log(do_animate);
if (do_animate) {
$(selector).fadeOut(300, function () {
$(this).fadeIn(300, function () {
should_blink(this, do_animate);
})})}
else {
$(this).fadeIn(300);
}
}
Looks like the problem is $(this).fadeIn(300);
看起来问题是
$(this).fadeIn(300);
, the $(this)
is not contained in a selected element... ,
$(this)
不在所选元素中...
if (do_animate) {
$(selector).fadeOut(300, function () {
$(this).fadeIn(300, function () {
should_blink(this, do_animate);
});
});
} else {
// $(this).fadeIn(300);
$(selector).fadeIn(300);
}
I solved this issue in a much simpler way by using CSS blink
: 我通过使用CSS
blink
以一种更简单的方式解决了这个问题:
@-webkit-keyframes blinker {
from {opacity: 1.0;}
to {opacity: 0.0;}
}
.blink{
text-decoration: blink;
-webkit-animation-name: blinker;
-webkit-animation-duration: 0.6s;
-webkit-animation-iteration-count:infinite;
-webkit-animation-timing-function:ease-in-out;
-webkit-animation-direction: alternate;
}
I then use jquery's respective AddClass
and RemoveClass
to trigger the animations. 然后,我使用jquery的各自的
AddClass
和RemoveClass
触发动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.