【问题标题】:Tooltip is getting cut off from the top inside table with vertical scroll工具提示被垂直滚动从内部表格的顶部切断
【发布时间】:2017-09-27 13:44:23
【问题描述】:

我有一个表格,对于每个表格单元格,我都显示了一个关于表格单元格悬停的工具提示。问题是当表格滚动并且工具提示内容非常大时,它会从顶部被切断。

我使用下面的 css 样式来显示工具提示。

.CellWithComment{
  position:relative;
}

.CellComment{
  display:none;
  position:absolute; 
  z-index:100;
  border:1px;
  background-color:white;
  border-style:solid;
  border-width:1px;
  border-color:red;
  padding:3px;
  color:red; 
  bottom:20px; 
  left:20px;
}

我做了一个小提琴 table with tooltip。 您可以将鼠标悬停在第一行的第一列以查看问题。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您走在正确的道路上。您可以从顶部定位,而不是从底部定位评论。我还在代码中添加了一些注释,见下文。

    #MyTable{
      border-style:solid;
      border-color:black;
      border-width:2px
    }
    
    #MyTable td{
      border-style:solid;
      border-color:black;
      border-width:1px;
      padding:3px;
    }
    
    .CellWithComment{
      position:relative;
    }
    
    .CellComment{
      display:none;
      position:absolute; 
      z-index:100;
      border:1px;
      background-color:white;
      border-style:solid;
      border-width:1px;
      border-color:red;
      padding:3px;
      color:red; 
      bottom:20px; 
      left:20px;
      width: 130px; 
      max-height: 37px; 
      overflow-x: auto;
    }
    
    .CellWithComment:hover span.CellComment{
      display:block;
    }
    <div style="height:200px;overflow:auto;margin-top:40px">
    <table id="MyTable">
      <caption>Cell 1,2 Has a Comment</caption>
      <thead>
        <tr>
          <td>Heading 1</td>
          <td>Heading 2</td>
          <td>Heading 3</td>
        </tr>
      </thead>
      <tbody>
        <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">That is going to be a really big tool-tip text. </span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
            <tr>
         <td class="CellWithComment">Cell 1,2
            <span class="CellComment">Here is a comment</span>
          </td>
          <td>Cell 1,1</td>
         
          <td>Cell 1,3</td>
          </tr>
        <tr>
          <td>Cell 2,1</td>
          <td>Cell 2,2</td>
          <td>Cell 2,3</td>
        </tr>
      </tbody>
      
    </table>
    </div>

    【讨论】:

    • 你好 blecaf。其实这不是我的要求。我想在表格单元格内容上方显示工具提示。这就是我使用底部的原因。无论工具提示内容的长度如何,它都应该始终可见。
    • 建议在评论和最大高度上设置溢出。更新了我的代码。我不认为我知道任何其他方式可以做到这一点而不看起来很奇怪也许 js
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2015-05-30
    • 2020-04-04
    • 2014-01-15
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多