37

我正在尝试创建一个表格来显示个人的 BMI。

作为其中的一部分,我希望 on :hover,<tr> <col>(or <colgroup>) 也被突出显示,以使交叉点更加明显。

由于该表将同时具有公制和英制测量值,因此 :hover 不必在单元格处停止(从任何方向),事实上,如果它从一个轴延伸到另一个轴,将是一个奖励。我也在使用 XHTML 1.1 Strict doctype,如果这有什么不同?

所以......一个例子(真正的桌子......更大),但这应该是有代表性的:

<script>

tr:hover {background-color: #ffa; }

colgroup:hover,
col:hover {background-color: #ffa; }

</script>

...

<table>
    <col class="weight"></col><colgroup span="3"><col class="bmi"></col></colgroup>

    <tr>
        <th></th>
        <th>50kg</th>
        <th>55kg</th>
        <th>60kg</th>
    </tr>

    <tr>
        <td>160cm</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
    </tr>

    <tr>
        <td>165cm</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
    </tr>

    <tr>
        <td>170cm</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
    </tr>

</table>

我在问不可能吗,我需要去 JQuery-wards 吗?

4

5 回答 5

65

这是一个不使用 JavaScript 的纯 CSS 方法。

我使用::before::after伪元素来突出显示行和列。如果您需要处理点击事件,请z-index在 s 下方突出显示。允许他们离开的范围。上隐藏高光溢出。<td>position: absolute<td>overflow: hidden<table>

这不是必需的,但是当您在标题中时,我也让它只选择行或列。.row.col类负责这一点。如果您希望简化,可以删除它们。

这适用于所有现代浏览器(并且在旧浏览器上无所作为而优雅地降级)。

演示:http: //jsfiddle.net/ThinkingStiff/rUhCa/

输出:

具有突出显示的行和列的 HTML 表格

CSS:

table {
    border-spacing: 0;
    border-collapse: collapse;
    overflow: hidden;
    z-index: 1;
}

td, th, .row, .col {
    cursor: pointer;
    padding: 10px;
    position: relative;
}

td:hover::before,
.row:hover::before { 
    background-color: #ffa;
    content: '\00a0';  
    height: 100%;
    left: -5000px;
    position: absolute;  
    top: 0;
    width: 10000px;   
    z-index: -1;        
}

td:hover::after,
.col:hover::after { 
    background-color: #ffa;
    content: '\00a0';  
    height: 10000px;    
    left: 0;
    position: absolute;  
    top: -5000px;
    width: 100%;
    z-index: -1;        
}

HTML:

<table>
    <tr>
        <th></th>
        <th class="col">50kg</th>
        <th class="col">55kg</th>
        <th class="col">60kg</th>
        <th class="col">65kg</th>
        <th class="col">70kg</th>
    </tr>
    <tr>
        <th class="row">160cm</th>
        <td>20</td><td>21</td><td>23</td><td>25</td><td>27</td>
    </tr>
    <tr>
        <th class="row">165cm</th>
        <td>18</td><td>20</td><td>22</td><td>24</td><td>26</td>
    </tr>
    <tr>
        <th class="row">170cm</th>
        <td>17</td><td>19</td><td>21</td><td>23</td><td>25</td>
    </tr>
    <tr>
        <th class="row">175cm</th>
        <td>16</td><td>18</td><td>20</td><td>22</td><td>24</td>
    </tr>
</table>
于 2012-06-24T08:16:51.717 回答
8

我在这里遇到了一个非常不错的 jQuery 插件它通过大量示例在这类事情上做得非常好。我会优先使用它。

于 2009-05-11T16:24:22.740 回答
4

无论如何,IE 不支持AFAIK CSS 悬停在TR's 上,所以它的 TR 部分充其量只能在 Firefox 中工作。

甚至从未见过:hover关于 col/colgroup 的作品,所以不确定这是否可能......

认为您可能会被 Javascript 实现所困。

这里有一个示例 在 Firefox 中有效(行和列),但在 IE 中又被破坏了……列不起作用。

于 2009-05-11T16:19:11.220 回答
2

我从css-tricks.com 遇到了这种巧妙的做法

//HTML

<table>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <colgroup></colgroup>
    <thead>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
</table>

//Js

$(function(){
    $("table").delegate('td','mouseover mouseleave', function(e) {
        if (e.type == 'mouseover') {
          $(this).parent().addClass("hover");
          $("colgroup").eq($(this).index()).addClass("hover");
        }
        else {
          $(this).parent().removeClass("hover");
          $("colgroup").eq($(this).index()).removeClass("hover");
        }
    });
})

在这里查看小提琴

于 2014-10-16T03:06:14.320 回答
1

现场回答(https://jsfiddle.net/craig1123/d7105gLf/

已经有 CSS 和 JQuery 的答案;但是,我写了一个简单的纯 javascript 答案。

我首先找到所有的coltd标签,通过do获得每个单元格的列索引element.cellIndex,然后添加一个背景为on的CSS类,然后将其mouseenter删除mouseleave

HTML

<table id='table'>
  <col />
  <col />
  <col />
  <col />
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Birthdate</th>
      <th>Preferred Hat Style</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Abraham Lincoln</td>
      <td>204</td>
      <td>February 12</td>
      <td>Stovepipe</td>
    </tr>
    <tr>
      <td>Winston Churchill</td>
      <td>139</td>
      <td>November 30</td>
      <td>Homburg</td>
    </tr>
    <tr>
      <td>Rob Glazebrook</td>
      <td>32</td>
      <td>August 6</td>
      <td>Flat Cap</td>
    </tr>
  </tbody>
</table>

CSS

body {
  font: 16px/1.5 Helvetica, Arial, sans-serif;
}

table {
  width: 80%;
  margin: 20px auto;
  border-collapse: collapse;
}
table th {
  text-align: left;
}
table tr, table col {
  transition: all .3s;
}
table tbody tr:hover {
  background-color: rgba(0, 140, 203, 0.2);
}
table col.hover {
  background-color: rgba(0, 140, 203, 0.2);
}
tr, col {
  transition: all .3s;
}
tbody tr:hover {
  background-color: rgba(0,140,203,.2);
}
col.hover {
  background-color: rgba(0,140,203,.2);
}

JS

const col = table.getElementsByTagName('col');
const td = document.getElementsByTagName('td');

const columnEnter = (i) => col[i].classList.add('hover');
const columnLeave = (i) => col[i].classList.remove('hover');

for (const cell of td) {
    const index = cell.cellIndex;
    cell.addEventListener('mouseenter', columnEnter.bind(this, index));
    cell.addEventListener('mouseleave', columnLeave.bind(this, index));
}

这是一个小提琴

于 2017-03-07T21:45:25.920 回答