CSS高亮表格行列无效

标签 css

我有 4 个并排的 table ,就像 Highlight column and row in a table ::after and ::before not work 中的那个一样

我的问题是,以该问题中的表格为例,当鼠标从 c 移动到 b 或 a(或从 b 移动到 a)时,突出显示停留在 c 所在的行,而不是移动到 b 或 a (与 b) 保持一致。基本上,当鼠标从上到下移动时效果很好,但从下到上移动时效果不佳。

我想知道如何解决它。因为我也让表格单元格在点击时高亮显示,并记录哪个单元格高亮和不高亮,所以问题有时会在点击时高亮错误的单元格,从而导致记录有偏差。

我正在使用 Chrome 进行测试。

举个例子

            th{
                font-weight: normal;
                align-content: center;
            }
            td {
                border: black solid;
                border-width: 1px 2px 1px 1px;
                padding: 5px;
                text-align: center;
                height:1.1cm;
                width:1.1cm;
                background-clip: padding-box

            }
            .col {
                width: 1.1cm;
            }
            .rotate {
                text-align: center;
                white-space: nowrap;
                 vertical-align: middle;
                width: 1.5em;
                }
            .rotate div {
                -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
                -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
                -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
                filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
                -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
                margin-left: -1cm;
                margin-right: -1cm;
                }
            .float-left {
                width: 25%;
                float: left;
            }
            tr.hl:hover th:not([rowspan]),
            tr.hl:hover td {
                background-color: rgba(255,255,0,0.25);
            }
            td:hover::before {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                width: 100%;
                top: 0;
            }
            td:hover::after,
            .col:hover::after {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                height: 2000%;
                width: 100%;
                bottom: -1000%;
                left: 0;
            }
            th,
            td {
                position: relative;
            }
            table{
                overflow: hidden;

            }
            thead th{
                background-color:white;
                box-shadow: 0 0 0 2px white;
                z-index: 1;
            }
           
<div class="g1g2">
        <div class="float-left">
            <table id="player1" style="margin-bottom: 1cm">
             <thead>
        <tr>
            <th colspan=5 style="text-align: right" >Player 1's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 2's actions</th>
        </tr>
        </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">d</th>
            <th class="col">e</th>
            <th class="col">f</th>
        </tr>


        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller;"> <div>Player 1's actions</div></th>
            <th>a</th>
            <td>8</td>
            <td>20</td>
            <td>12</td>
        </tr>

        <tr class="hl">
            <th>b</th>
            <td>0</td>
            <td>8</td>
            <td>16</td>
        </tr>
        <tr class="hl">
            <th>c</th>
            <td>18</td>
            <td>12</td>
            <td>6</td>
        </tr>
            </table>
        </div>
    </div>

    <div class="g1g2">
             <div class="float-left">
         <table id="player2" style="margin-bottom: 1cm">
             <thead>
             <tr>
            <th colspan=5 style="text-align: right">Player 2's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 3's actions</th>
        </tr>
             </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">g</th>
            <th class="col">h</th>
            <th class="col">i</th>
        </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 2's actions</div></th>
            <th>d</th>
            <td>14</td>
            <td>18</td>
            <td>4</td>
        </tr>
        <tr class="hl">
            <th>e</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>f</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
         </table>
     </div>
    </div>


    <div class="g1g2">
        <div class="float-left">
         <table id="player3" style="margin-bottom: 1cm">
             <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 3's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 4's actions</th>
            </tr>
             </thead>
             <tr style="text-align: center">
                  <th colspan="2"></th>
                <th class="col">j</th>
                <th class="col">k</th>
                <th class="col">l</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 3's actions</div></th>
            <th>g</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>h</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
        <tr class="hl">
            <th>i</th>
            <td>0</td>
            <td>16</td>
            <td>16</td>
        </tr>
         </table>
    </div>
    </div>

<div class="g1">
<div class="float-left">
         <table id="player4a" style="margin-bottom: 1cm">
              <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 4's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 1's actions</th>
            </tr>
             </thead>
            <tr style="text-align: center">
                 <th colspan="2"></th>
                <th class="col">a</th>
                <th class="col">b</th>
                <th class="col">c</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 4's actions</div></th>
            <th>j</th>
            <td>12</td>
            <td>16</td>
            <td>14</td>
        </tr>
        <tr class="hl">
            <th>k</th>
            <td>8</td>
            <td>12</td>
            <td>10</td>
        </tr>
        <tr class="hl">
            <th>l</th>
            <td>6</td>
            <td>10</td>
            <td>8</td>
        </tr>
         </table>
</div>
</div>

最佳答案

最简单的解决方法是添加 pointer-events: none ::after::before 伪元素。 It also has a great browser support .

老实说,我还没有看过代码 xD,但可能另一个修复方法是乱用 z-index,我不推荐,因为它可以用 解决指针事件

            th{
                font-weight: normal;
                align-content: center;
            }
            td {
                border: black solid;
                border-width: 1px 2px 1px 1px;
                padding: 5px;
                text-align: center;
                height:1.1cm;
                width:1.1cm;
                background-clip: padding-box

            }
            .col {
                width: 1.1cm;
            }
            .rotate {
                text-align: center;
                white-space: nowrap;
                 vertical-align: middle;
                width: 1.5em;
                }
            .rotate div {
                -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
                -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
                -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
                filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
                -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
                margin-left: -1cm;
                margin-right: -1cm;
                }
            .float-left {
                width: 25%;
                float: left;
            }
            tr.hl:hover th:not([rowspan]),
            tr.hl:hover td {
                background-color: rgba(255,255,0,0.25);
            }
            td:hover::before {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                width: 100%;
                top: 0;
            }
            td:hover::after,
            .col:hover::after {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                height: 2000%;
                width: 100%;
                bottom: -1000%;
                left: 0;
                pointer-events: none;
            }
            th,
            td {
                position: relative;
            }
            table{
                overflow: hidden;

            }
            thead th{
                background-color:white;
                box-shadow: 0 0 0 2px white;
                z-index: 1;
            }
           
<div class="g1g2">
        <div class="float-left">
            <table id="player1" style="margin-bottom: 1cm">
             <thead>
        <tr>
            <th colspan=5 style="text-align: right" >Player 1's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 2's actions</th>
        </tr>
        </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">d</th>
            <th class="col">e</th>
            <th class="col">f</th>
        </tr>


        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller;"> <div>Player 1's actions</div></th>
            <th>a</th>
            <td>8</td>
            <td>20</td>
            <td>12</td>
        </tr>

        <tr class="hl">
            <th>b</th>
            <td>0</td>
            <td>8</td>
            <td>16</td>
        </tr>
        <tr class="hl">
            <th>c</th>
            <td>18</td>
            <td>12</td>
            <td>6</td>
        </tr>
            </table>
        </div>
    </div>

    <div class="g1g2">
             <div class="float-left">
         <table id="player2" style="margin-bottom: 1cm">
             <thead>
             <tr>
            <th colspan=5 style="text-align: right">Player 2's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 3's actions</th>
        </tr>
             </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">g</th>
            <th class="col">h</th>
            <th class="col">i</th>
        </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 2's actions</div></th>
            <th>d</th>
            <td>14</td>
            <td>18</td>
            <td>4</td>
        </tr>
        <tr class="hl">
            <th>e</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>f</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
         </table>
     </div>
    </div>


    <div class="g1g2">
        <div class="float-left">
         <table id="player3" style="margin-bottom: 1cm">
             <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 3's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 4's actions</th>
            </tr>
             </thead>
             <tr style="text-align: center">
                  <th colspan="2"></th>
                <th class="col">j</th>
                <th class="col">k</th>
                <th class="col">l</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 3's actions</div></th>
            <th>g</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>h</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
        <tr class="hl">
            <th>i</th>
            <td>0</td>
            <td>16</td>
            <td>16</td>
        </tr>
         </table>
    </div>
    </div>

<div class="g1">
<div class="float-left">
         <table id="player4a" style="margin-bottom: 1cm">
              <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 4's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 1's actions</th>
            </tr>
             </thead>
            <tr style="text-align: center">
                 <th colspan="2"></th>
                <th class="col">a</th>
                <th class="col">b</th>
                <th class="col">c</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 4's actions</div></th>
            <th>j</th>
            <td>12</td>
            <td>16</td>
            <td>14</td>
        </tr>
        <tr class="hl">
            <th>k</th>
            <td>8</td>
            <td>12</td>
            <td>10</td>
        </tr>
        <tr class="hl">
            <th>l</th>
            <td>6</td>
            <td>10</td>
            <td>8</td>
        </tr>
         </table>
</div>
</div>

关于CSS高亮表格行列无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57937149/

相关文章:

html - css text-align 文本之间的间隙

javascript - 在页面构建器 WP 上自动删除斜杠

css - css中的正则表达式是否符合W3C标准?

css - CSS中的div透明动画

css - 垂直对齐 :bottom WITH float:left

css - 使绝对定位的 child 不会超出视口(viewport)边界

javascript - css 响应式下拉菜单 - 更多级别

html - 尝试在将鼠标悬停在菜单项上时在底部显示 div

javascript - jQuery 中的随机颜色

html - 背景图像的阴影