表头中的工具提示

时间:2017-12-04 06:49:45

标签: css header tooltip fixed

我有一个可滚动的表,其中包含固定的标题和列,以及标题中的工具提示。标题中还有一个rowspan。我已经尝试了很长时间来获取标题行中的工具提示,以显示以下行中标题单元格的顶部。即使在阅读了堆叠上下文和z-index(包括W3C规范)后可以找到的所有内容之后,我似乎无法找到解决方案。

CSS:

.gradebooktable {
  overflow: auto;
  height: 200px;
  width: 400px
}

table {
  table-layout: fixed;
  border-width: 0;
  border-spacing: 0;
}

td {
  position:relative;
  padding: 3px;
  white-space: nowrap;
  border-right: 1px solid #ccc;
}

th {
  position:relative;
  z-index:20; 
  background: #999;
}

th.pinned {
  position: relative; 
  z-index: 40;     
  background: #ccc;
}

td.pinned {
  position: relative;
  z-index: 30; 
  background: #eee; 
}

.tooltip{
  position:relative;
}

.tooltiptext{
  display:none;
  position:absolute; 
  z-index:10;
  border:1px;
  background-color:#eee;
  border-style:solid;
  border-width:1px;
  border-color:blue;
  border-radius: 6px;
  padding:3px;
  color:blue; 
  top:20px; 
  left:20px;
}

.tooltip:hover span.tooltiptext{
  display:block;
}

HTML:

<BODY>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<SCRIPT>
$( function() {
$('#gradebooktable').scroll( function() {
      var translate = "translate(0," + this.scrollTop + "px)";
      $("table thead th:not(.pinned)").css('transform', translate);

      translate = "translate(" + this.scrollLeft + "px,0)";
      $("table tbody .pinned").css('transform', translate);

      translate = "translate(" + this.scrollLeft + "px," + this.scrollTop + "px)";
      $("table thead th.pinned").css('transform', translate);
    }
);  
});
</SCRIPT>
  <div id="gradebooktable" class="gradebooktable">
    <table>
      <thead>
        <tr>
          <th class="pinned" rowspan=3>Col 0</th>
          <th class="pinned" rowspan=3>Col 1</th>
          <th class="tooltip">Col 2A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 3A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 4A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 5A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 6A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 7A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 8A
               <span class="tooltiptext">Tooltip for header cell</span></th>
          <th class="tooltip">Col 9A
               <span class="tooltiptext">Tooltip for header cell</span></th>
        </tr>
        <tr>
          <th>Col 2B</th>
          <th>Col 3B</th>
          <th>Col 4B</th>
          <th>Col 5B</th>
          <th>Col 6B</th>
          <th>Col 7B</th>
          <th>Col 8B</th>
          <th>Col 9B</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
          <td class="tooltip">Another Cell
                  <span class="tooltiptext">Tooltip for body cell</span></td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
        <tr>
          <td class="pinned">First Cell</td>
          <td class="pinned">Second Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
          <td>Another Cell</td>
        </tr>
      </tbody>
    </table>
    </div>
</BODY>

在这里小提琴:https://jsfiddle.net/k42myms3/5/

目标是将跨区标题行的顶行中的工具提示显示在跨区标题行底行的单元格顶部。我可以找到方法来显示工具提示,例如从.tooltiptext css中删除position:absolute,但不会导致定位问题或者破坏在固定标题和列后面滚动的表格单元格。

1 个答案:

答案 0 :(得分:0)

没关系,修好它:https://jsfiddle.net/k42myms3/10/

已添加代码:

th.tooltip{
  position:relative;
  z-index: 35; 
}

......以及其他一些事情。