gpt4 book ai didi

javascript - 在溢出隐藏容器外显示元素

转载 作者:技术小花猫 更新时间:2023-10-29 12:53:44 25 4
gpt4 key购买 nike

我有一个 <table>通过将其包装在 <div> 中使其可滚动固定高度。

<table>有一个下拉组件(蓝色容器在 2nd 列的 1st 图像中的行和下面给出的 jsfiddle)隐藏在容器后面 <div> .我希望它显示所有选项。

current output

JSFIDDLE (例子)

如何将下拉组件放到容器外<div>显示下图中的所有选项?

desired output

如果我删除 pRelative容器,则下拉菜单完全可见 - 但是当我滚动时,下拉菜单不会与其容器一起滚动。

提前致谢。

PS:只寻找 CSS/javascript 解决方案。

最佳答案

您可以将下拉列表的位置更改为fixed 并使用 js 处理滚动,如下所示。

var main = document.getElementsByClassName('main')[0];
var dd = document.getElementsByClassName('pAbsolute')[0];
var offset = dd.getBoundingClientRect().top;
main.onscroll = function() {
var st = main.scrollTop;
ddt = (offset - st);
dd.style.top = ddt + 'px';
}
.main {
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
}
.pRelative {
position: relative;
}
.pAbsolute {
position: fixed;
}
.dropdown {
width: 100px;
background-color: cornflowerblue;
z-index: 1000;
}
.option {
border-top: 1px solid green;
border-bottom: 1px solid green;
}
table td {
border: 1px solid black;
padding: 10px;
}
<div class="main">
<table>
<tr>
<td>row1 column1</td>
<td>
<div class="pRelative">
<div class="pAbsolute dropdown">
<div class="option">Zero</div>
<div class="option">One</div>
<div class="option">Two</div>
<div class="option">Three</div>
<div class="option">Four</div>
<div class="option">Five</div>
<div class="option">Six</div>
</div>
</div>
</td>
<td>row1 column3</td>
</tr>
<tr>
<td>row2 column1</td>
<td>row2 column2</td>
<td>row2 column3</td>
</tr>
<tr>
<td>row3 column1</td>
<td>row3 column2</td>
<td>row3 column3</td>
</tr>
<tr>
<td>row4 column1</td>
<td>row4 column2</td>
<td>row4 column3</td>
</tr>
<tr>
<td>row5 column1</td>
<td>row5 column2</td>
<td>row5 column3</td>
</tr>
<tr>
<td>row6 column1</td>
<td>row6 column2</td>
<td>row6 column3</td>
</tr>
<tr>
<td>row7 column1</td>
<td>row7 column2</td>
<td>row7 column3</td>
</tr>
<tr>
<td>row8 column1</td>
<td>row8 column2</td>
<td>row8 column3</td>
</tr>
</table>
</div>

Demo

更新

您可以通过创建新的堆叠上下文来解决 margin-top 问题。

(仅在 safari 6.1 mac 中测试 - 不幸的是在任何最新的浏览器中都不起作用) Updated DemoAnother Demo

更新

我能找到的隐藏固定元素溢出的唯一可能的跨浏览器解决方法是剪辑容器(这要求它是定位元素)

var main = document.getElementsByClassName('main')[0];
var dd = document.getElementsByClassName('pAbsolute')[0];
var offset = dd.getBoundingClientRect().top;
main.onscroll = function() {
var st = main.scrollTop;
ddt = (offset - st);
dd.style.top = ddt + 'px';
}
.main {
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
margin-top: 100px;
position: absolute;
clip: rect(auto, auto, 99999px, auto);
}
.pRelative {
position: relative;
}
.pAbsolute {
position: fixed;
}
.dropdown {
width: 100px;
background-color: cornflowerblue;
z-index: 1000;
}
.option {
border-top: 1px solid green;
border-bottom: 1px solid green;
}
table td {
border: 1px solid black;
padding: 10px;
}
<div class="main">
<table>
<tr>
<td>row1 column1</td>
<td>
<div class="pRelative">
<div class="pAbsolute dropdown">
<div class="option">Zero</div>
<div class="option">One</div>
<div class="option">Two</div>
<div class="option">Three</div>
<div class="option">Four</div>
<div class="option">Five</div>
<div class="option">Six</div>
</div>
</div>
</td>
<td>row1 column3</td>
</tr>
<tr>
<td>row2 column1</td>
<td>row2 column2</td>
<td>row2 column3</td>
</tr>
<tr>
<td>row3 column1</td>
<td>row3 column2</td>
<td>row3 column3</td>
</tr>
<tr>
<td>row4 column1</td>
<td>row4 column2</td>
<td>row4 column3</td>
</tr>
<tr>
<td>row5 column1</td>
<td>row5 column2</td>
<td>row5 column3</td>
</tr>
<tr>
<td>row6 column1</td>
<td>row6 column2</td>
<td>row6 column3</td>
</tr>
<tr>
<td>row7 column1</td>
<td>row7 column2</td>
<td>row7 column3</td>
</tr>
<tr>
<td>row8 column1</td>
<td>row8 column2</td>
<td>row8 column3</td>
</tr>
</table>
</div>

Demo

关于javascript - 在溢出隐藏容器外显示元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24301400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com