我在overflow:scroll容器中有一个表,表中有一些按钮,当有人单击它们时,它们会显示上下文/工具提示(位置:绝对层)文本。

当我向右滚动并单击按钮时,它会向右移动到外部而忽略滚动:

css - 溢出:滚动div,位置为:绝对元素-LMLPHP

使容器位置相对可以解决位置问题,但是它出现在容器内部而不显示菜单:

css - 溢出:滚动div,位置为:绝对元素-LMLPHP

我需要帮助来获得以下期望的行为:

css - 溢出:滚动div,位置为:绝对元素-LMLPHP

这是代码段:



.container{
  width:200px;
  height:100px;
  overflow:scroll;
  position:relative; /* removing this solves the problem, but .contextual moves to the original position */
}
.board{
  width:400px;
}
.contextual{
  display:none;
  position:absolute;
  width:100px;
  height:100px;
  margin: 20px;
  z-index: 2;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=container>
    <table class=board>
      <tr><td colspan=2>This board size (200) is bigger than its container size (100).</td></tr>
      <tr>
        <td>this is a button with a contextual element</td>
        <td>
          <input type=button value="click me" onclick="$('.contextual').show();" />
          <div class=contextual>This is a contextual help text.</div>
        </td>
      </tr>
    </table>
</div>

最佳答案

将上下文div置于溢出的div之外,并根据鼠标位置进行定位。



showContext = function() {
    var e = window.event;

    var posX = e.clientX;
    var posY = e.clientY;
    var context = document.getElementById("contextual")
    context.style.top = posY + "px";
    context.style.left = posX + "px";
    context.style.display = "block";
}

.container{
  width:200px;
  height:100px;
  overflow:scroll;
  position:relative; /* removing this solves the problem, but .contextual moves to the original position */
  z-index:1;
}
.board{
  width:400px;
}
#contextual{
  display:none;
  position:absolute;
  width:100px;
  height:100px;
  background-color:grey;
  z-index: 2;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <table class="board">
      <tr><td colspan=2>This board size (200) is bigger than its container size (100).</td></tr>
      <tr>
        <td>this is a button with a contextual element</td>
        <td>
          <input type="button" value="click me" onclick="javascript:showContext();" />

        </td>
      </tr>
    </table>
</div>
<div id="contextual">This is a contextual help text.</div>

09-17 14:35
查看更多