JQuery 中闪烁效果的动画颜色变化

发布于 2024-10-16 13:30:55 字数 719 浏览 2 评论 0原文

我只是花了相当多的时间试图让一些文本闪烁,通过让它从一种颜色来回改变到另一种颜色。听起来很简单,结果却非常困难。简单的事情就像这样:

$('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-10-23 13:30:55

链接你的动画

$('#myElement').animate({color:black}, 100).animate({color:white}, 100);

如果你想停止运行动画,请使用 jQuery.stop()

$('#myElement').stop();

Chain your animations

$('#myElement').animate({color:black}, 100).animate({color:white}, 100);

If you want to stop running animation use jQuery.stop()

$('#myElement').stop();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文