如何在jquery中使div背景颜色出现和消失?

发布于 2024-08-22 04:51:54 字数 406 浏览 4 评论 0原文

我正在尝试让 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 技术交流群。

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

发布评论

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

评论(3

找个人就嫁了吧 2024-08-29 04:51:54

您已经链接了 fadein()fadeout()

$("#second_div_ID").fadeIn(30).css("background-color", 'blue').fadeOut(1000).css("background-color", 'blue'); 

因此它们很可能被同步调用。

您可能还在寻找animate()

以确保一个在另一个完成时被调用。试试这个:

var $second_div = $("#second_div_ID");
var oldBGColour = $second_div.css('background-color');

$second_div.animate({'background-color': 'blue'}, 30, function(){
    $(this).animate({background-color: oldBGColour}, 1000);
})

未经测试,但你会想要类似上面的东西

You have chained your fadein() and fadeout():

$("#second_div_ID").fadeIn(30).css("background-color", 'blue').fadeOut(1000).css("background-color", 'blue'); 

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:

var $second_div = $("#second_div_ID");
var oldBGColour = $second_div.css('background-color');

$second_div.animate({'background-color': 'blue'}, 30, function(){
    $(this).animate({background-color: oldBGColour}, 1000);
})

Not tested, but you'll want something like the above

八巷 2024-08-29 04:51:54

当您使用 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.

哥,最终变帅啦 2024-08-29 04:51:54
<div class="quick-alert">Alert! Watch me before it's too late!</div>

.quick-alert {
   width: 50%;
   margin: 1em 0;
   padding: .5em;
   background: #ffa;
   border: 1px solid #a00;
   color: #a00;
   font-weight: bold;
   display: none;
 }

$(document).ready(function() {
  $('#show-alert').click(function() {
    $('<div class="quick-alert">Alert! Watch me before it\'s too late!</div>')
    .insertAfter( $(this) )
    .fadeIn('slow')
    .animate({opacity: 1.0}, 3000)
    .fadeOut('slow', function() {
      $(this).remove();
    });
  });
});

DIV 是显示对象,一旦您单击按钮(通过 ID #show-alert)此消息就会出现,并在 3 秒后自动消失。它的css也附上了,

希望有用,

干杯!

<div class="quick-alert">Alert! Watch me before it's too late!</div>

.quick-alert {
   width: 50%;
   margin: 1em 0;
   padding: .5em;
   background: #ffa;
   border: 1px solid #a00;
   color: #a00;
   font-weight: bold;
   display: none;
 }

$(document).ready(function() {
  $('#show-alert').click(function() {
    $('<div class="quick-alert">Alert! Watch me before it\'s too late!</div>')
    .insertAfter( $(this) )
    .fadeIn('slow')
    .animate({opacity: 1.0}, 3000)
    .fadeOut('slow', function() {
      $(this).remove();
    });
  });
});

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!

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