表tr背景渐变在使用CSS的悬停

时间:2015-11-03 02:36:45

标签: html css css3 linear-gradients

我的要求:

tr悬停时,tr将具有线性渐变背景,不仅是平坦的背景颜色

td中的tr示例:

A  |  B  |  C  |  B  |  A

A darker --> C lighter

的jsfiddle:

https://jsfiddle.net/g7o84j43/

代码我尝试过:

HTML:

<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
<tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
</tr>
<tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
</tr>
</table>

CSS:

tr:hover
{
    background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}

任何想法如何实现这一目标?

2 个答案:

答案 0 :(得分:8)

tr上为hover设置渐变背景的最简单,最直接的解决方案是向其添加background-attachment: fixed

&#13;
&#13;
tr:hover {
  background: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
  background-attachment: fixed;
}
&#13;
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>
&#13;
&#13;
&#13;

这是一种在将鼠标悬停在行上时向行(tr)添加渐变背景的不同方法。在这种方法中,我们将渐变作为背景添加到table元素本身,然后将未覆盖状态覆盖为纯色(白色)。当tr悬停时,tr的背景设置为transparent,因此table背景会显示渐变。

&#13;
&#13;
table {
  background: linear-gradient(to right, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
}
tr {
  background: white;
}
tr:hover {
  background: transparent;
}
&#13;
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>
&#13;
&#13;
&#13;

如果较浅的颜色需要位于中心,则可以修改渐变,如下面的代码段所示:

&#13;
&#13;
table {
  background: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
}
tr {
  background: white;
}
tr:hover {
  background: transparent;
}
&#13;
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:4)

也许不是最好的解决方案,但也许对你来说足够好,而且它也在Chrome中工作。 DEMO

<强> HTML:

<div class="table-wrapper">
    <table>
        ...
    </table>
</div>

<强> CSS:

/* To hide the overflow */
.table-wrapper{
    position: relative;
    overflow: hidden;
}

tr{
    position: relative;
}

/* Making the background with :before pseudo-element */

tr:before
{
    position: absolute;
    content: "";
    width: 100%;
    height: 20px;
    z-index: -1;
}

tr:hover:before
{
    background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}