溢出:滚动 div,其中包含位置:绝对元素

str*_*min 10 css scroll css-position absolute

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

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

在此输入图像描述

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

在此输入图像描述

我需要帮助才能获得以下所需的行为:

在此输入图像描述

这是片段:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

lou*_*rtz 5

对于遇到此问题但设置鼠标位置无济于事的人

如果您遇到与我相同的问题,尽管父容器被设置为overflow: scroll并且父元素的子元素被切断position: absolute- 执行以下操作

例如,构建一个下拉菜单的用例

  • 将元素设置为position: fixed
  • 获取父元素的视口位置
  • 将位置topleftbottomright值设置为视口值

您可以使用与 @jfeferman 的代码片段相同的代码


jfe*_*man 3

将上下文 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";
}
Run Code Online (Sandbox Code Playgroud)
.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)