JQuery 中闪烁效果的动画颜色变化
我只是花了相当多的时间试图让一些文本闪烁,通过让它从一种颜色来回改变到另一种颜色。听起来很简单,结果却非常困难。简单的事情就像这样:
$('#myElement').animate({color:black}, 100);
$('#myElement').animate({color:white}, 100);
将其放入一个循环中,重复五次,我们就完成了。好吧,即使安装了 Jquery 颜色插件,也不起作用(这确实会阻止浏览器报告错误,但不会使代码工作......)。
有效的是这个:
$('myElement').animate({top:0}, 100, function(){$('myElement').css('color','#000000');});
$('myElement').animate({top:0}, 100, function(){$('myElement'.css('color','#ffffff');});
也就是说:在 animate 函数中根本不做任何事情,除了将其用作计时器,然后更改元素上的 css 颜色值。就是这样。工作正常。
顺便说一句:如果你这样做并想在颜色闪烁时阻止其他事情发生,你将必须放置某种计时器或标志 - 请记住,浏览器将执行多任务,并且将在闪烁颜色时继续并行执行颜色在闪烁。因此,如果您想在该菜单选项闪烁时禁止用户选择另一个菜单选项,则必须禁用其他选项,直到该动画完成为止。
I just spent quite some time trying to get some text to flicker, by having it change back and forth from one color to another. Sounds simple, turns out to be horridly difficult. The simple thing would be something like:
$('#myElement').animate({color:black}, 100);
$('#myElement').animate({color:white}, 100);
Put this in a loop, repeat five times say, we are done. Well, does not work, even with the Jquery color plug in installed (which does stop browsers reporting errors but does not make the code work..).
What does work is this one:
$('myElement').animate({top:0}, 100, function(){$('myElement').css('color','#000000');});
$('myElement').animate({top:0}, 100, function(){$('myElement'.css('color','#ffffff');});
That is: don't do anything at all in the animate function, except use it as a timer, then change the css color value on the element. That's it. Works fine.
By the way: if you do this and want to stop something else happening while you are flickering the colors, you will have to put some kind of timer or flag in place -- remember that browsers will multitask and will go on executing in parallel while the colors are flickering. So if you want for example to disable the user choosing another menu choice while this one flickers, you will have to do this by disabling the other choices till this animation is done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接你的动画
如果你想停止运行动画,请使用 jQuery.stop()
Chain your animations
If you want to stop running animation use jQuery.stop()