我的要求:
在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) */
}
任何想法如何实现这一目标?
答案 0 :(得分:8)
在tr
上为hover
设置渐变背景的最简单,最直接的解决方案是向其添加background-attachment: fixed
。
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;
这是一种在将鼠标悬停在行上时向行(tr
)添加渐变背景的不同方法。在这种方法中,我们将渐变作为背景添加到table
元素本身,然后将未覆盖状态覆盖为纯色(白色)。当tr
悬停时,tr
的背景设置为transparent
,因此table
背景会显示渐变。
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;
如果较浅的颜色需要位于中心,则可以修改渐变,如下面的代码段所示:
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;
答案 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) */
}