jQuery 淡入淡出背景颜色

发布于 2024-08-29 07:12:27 字数 248 浏览 2 评论 0 原文

我正在尝试淡入表格行的背景颜色,但无法做到正确。单击按钮时将发生淡入。

我尝试了类似的方法:

$("#row_2").fadeIn('slow').css('background', 'gold')

虽然这会将颜色应用到表格行,但它不会淡入,而是立即应用。

我确信这是一件简单的事情,但我找不到答案。我已经浏览过这个网站,但对于这个特定的事情仍然没有运气。

提前致谢

I'm trying to fade in the background colour of a table row, and can't get it right. The fade-in will happen when a button is clicked.

I tried something like:

$("#row_2").fadeIn('slow').css('background', 'gold')

And although this will apply the colour to the table row, it won't fade in, but apply it at once.

I'm sure this is a simple thing, but I can't find an answer to that. I've looked all over in this website, but still no luck for this specific thing.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

她说她爱他 2024-09-05 07:12:27

纯 jQuery 不具有动画颜色的功能。您必须使用 jQueryUIjQuery 颜色插件

然后使用 .animate() 函数。

The pure jQuery does not have functionality to animate colors. You have to use jQueryUI or jQuery color plugin.

Then use .animate() function.

卖梦商人 2024-09-05 07:12:27

如果您还没有使用Peter Peller 就是正确的选择.com/" rel="nofollow noreferrer">jquery UI,那么至少使用 jQuery 颜色插件

下面是我编写的代码片段,它在各种浏览器中都取得了成功:

<a href="#" ID="fadeTable" title="click to fade col1">Click to fade Column 1</a>
<table width="400px"  border="1" cellspacing="0" cellpadding="1" 
                      summary="This is my test table">
  <caption align="top">
  My Caption
  </caption>
  <tr>
    <th scope="col" class="row0 col1" >Col 1</th><th scope="col">Col 2</th>
  </tr>
  <tr>
    <td class="row1 col1" >one</td><td>Uno</td>
  </tr>
  <tr>
    <td class="row2 col1" >two</td><td>Dos</td>
  </tr>
</table>
<script language="javascript" type="text/javascript">
 $(document).ready(function(){
    // requires http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js
    var iAniSpeed = 2000;
    var sBgColor = 'gold';
    $('#fadeTable').click(function(){
      $('td.col1').animate( { backgroundColor: sBgColor }, iAniSpeed);
        return false;       
    });
});
</script>

作为替代方案,您可能希望首先将背景着色为其原始颜色,然后将动画设置为新颜色。

要实现这一点,只需将当前的动画块替换为如下内容:

  $('td.col1').animate( { backgroundColor: 'white' }, 250, function() 
      {
        $(this).animate( { backgroundColor: 'gold' }, 2000);
      }
  );

Peter Peller is spot-on, if you're not already employing jquery UI, then at least go with the jQuery color plugin.

Below is a code snippet that I whipped-up which had success across a variety of browsers:

<a href="#" ID="fadeTable" title="click to fade col1">Click to fade Column 1</a>
<table width="400px"  border="1" cellspacing="0" cellpadding="1" 
                      summary="This is my test table">
  <caption align="top">
  My Caption
  </caption>
  <tr>
    <th scope="col" class="row0 col1" >Col 1</th><th scope="col">Col 2</th>
  </tr>
  <tr>
    <td class="row1 col1" >one</td><td>Uno</td>
  </tr>
  <tr>
    <td class="row2 col1" >two</td><td>Dos</td>
  </tr>
</table>
<script language="javascript" type="text/javascript">
 $(document).ready(function(){
    // requires http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js
    var iAniSpeed = 2000;
    var sBgColor = 'gold';
    $('#fadeTable').click(function(){
      $('td.col1').animate( { backgroundColor: sBgColor }, iAniSpeed);
        return false;       
    });
});
</script>

As an alternate, you may want to color the background to its original color first, then animate to the new color.

To make that happen, just replace the current animate block with something like this:

  $('td.col1').animate( { backgroundColor: 'white' }, 250, function() 
      {
        $(this).animate( { backgroundColor: 'gold' }, 2000);
      }
  );
撩起发的微风 2024-09-05 07:12:27

不幸的是,不可能淡入背景颜色(我不知道jquery的最新版本)。但是,您可以使用此插件来实现此目的:

highlightFade

现在由您决定是否使用该插件来实现背景淡入淡出效果:)

Unfortunately it is not possible to fade in the background color (I don't know about the latest release of jquery). However you can use this plugin for that purpose:

highlightFade

Now it's up to you whether you use that plugin or not for just background fading effect :)

舂唻埖巳落 2024-09-05 07:12:27

jquery 突出显示效果怎么样,如下所示:

$("div").click(function () {
      $(this).effect("highlight", {}, 3000);
});

您还可以指定突出显示的颜色和持续时间。您可以从 jquery 站点了解更多信息。

What about jquery highlight effect, like this:

$("div").click(function () {
      $(this).effect("highlight", {}, 3000);
});

Also you can specify color and duration it should be hightlighted. You can learn more from jquery site.

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