如何在jquery中使div背景颜色出现和消失?
我正在尝试让 div 的背景颜色出现然后消失,就像闪光一样来来去去,但到目前为止还没有成功。单击一个 div 时,我试图为另一个 div 的背景颜色提供闪光效果。
到目前为止,我的 jquery 知识已经达到以下代码:
$("div.first_div").click(function(){
$("#second_div_ID").fadeIn(30).css("background-color", 'blue')
.fadeOut(1000).css("background-color", 'blue');
}); });
但是发生的情况是整个第二个 div 以及意外的内容消失了。任何帮助将不胜感激!谢谢!
I am trying make the background color of a div appear and then disappear like a flash coming and going but without nay success till now. On the click of a div, I am trying to give a flash effect to another div's background color.
So far, my jquery knowledge have come to the following code:
$("div.first_div").click(function(){
$("#second_div_ID").fadeIn(30).css("background-color", 'blue')
.fadeOut(1000).css("background-color", 'blue');
}); });
but what happens is the whole second div disappears along with the content which is not expected. Any help would be appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经链接了
fadein()
和fadeout()
:因此它们很可能被同步调用。
您可能还在寻找
animate()
:以确保一个在另一个完成时被调用。试试这个:
未经测试,但你会想要类似上面的东西
You have chained your
fadein()
andfadeout()
:so they are likely to be called syncronously.
You are also maybe looking for
animate()
:To ensure one is called when the other has finished. try this:
Not tested, but you'll want something like the above
当您使用 fadeIn 和 fadeOut 时,您不仅会在该 div 的背景上使用 out,还会在整个元素(包括所有子元素)上使用 out。
因此,您需要将背景颜色设置为“蓝色”,然后将其设置为透明。在两者之间,您可能需要设置一点延迟。这可以通过以下插件来完成: http://www.evanbot.com/ Article/jquery-delay-plugin/4
也许你也可以使用这个插件: http:// /plugins.jquery.com/project/color
它使 jQuery 能够对颜色执行动画,也许这就是您所需要的。
When you use fadeIn and fadeOut you don't just use out on that div's background but on the entire element including all sub elements.
Therefore you need to set the background-color to 'blue' and then aftwards set it to transparent. In between the two you might want to set in a little delay. This can be accomplished with this plugin: http://www.evanbot.com/article/jquery-delay-plugin/4
Maybe you can also use this plugin: http://plugins.jquery.com/project/color
It gives jQuery the ability to perform animations on colors and maybe this is what you need.
DIV 是显示对象,一旦您单击按钮(通过 ID #show-alert)此消息就会出现,并在 3 秒后自动消失。它的css也附上了,
希望有用,
干杯!
DIV is the display object, once you click on the button (by the ID #show-alert) this msg will appear, and after 3sec automatically disappear. Its css also attached,
Hope useful,
Cheers!