jQuery淡入背景色

时间:2010-04-16 10:49:12

标签: jquery css fadein

我正试图淡化表格行的背景颜色,并且无法使其正确。单击按钮时会发生淡入。

我尝试过类似的事情:

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

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

我确信这是一件简单的事情,但我无法找到答案。我在这个网站上看了一遍,但是对于这个具体的事情仍然没有运气。

提前致谢

4 个答案:

答案 0 :(得分:18)

纯jQuery没有动画颜色的功能。您必须使用jQueryUIjQuery color plugin

然后使用.animate()函数。

答案 1 :(得分:9)

如果你还没有使用Peter Peller,那么

jquery UI是正确的,那么至少要使用jQuery color plugin

以下是我在各种浏览器中取得成功的代码片段:

<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);
      }
  );

答案 2 :(得分:1)

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

<强> highlightFade

现在由您决定是否使用该插件仅用于背景淡化效果:)

答案 3 :(得分:0)

如何使用jquery高亮效果:

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

您还可以指定颜色和持续时间应该高亮显示。您可以从jquery site了解更多信息。

相关问题