简体   繁体   English

如何使位置绝对尊重垂直视口限制

[英]How to make position absolute respect vertically viewport limits

I have a table with some rows and each row has a "more options" button that when clicked, opens a dropdown with the options.我有一个包含一些行的表格,每行都有一个“更多选项”按钮,单击该按钮时,会打开一个包含选项的下拉列表。 But in the last row, the options dropdown persists to overflow the viewport vertically.但在最后一行,选项下拉菜单仍然垂直溢出视口。 I saw a similar question here , but i'm not using bootstrap.我在这里看到了一个类似的问题,但我没有使用引导程序。 The Bootstrap solution used JS to calculate and position the element using transform. Bootstrap 解决方案使用 JS 来计算和使用变换定位元素。 But there is any way to do that with just CSS?但是有什么办法可以只用 CSS 做到这一点?

Here its my table structure:这是我的表结构:

    <table>
      <tbody>
        <tr>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                ...
            </td>
            <td>
                <svg class="ICON HERE..."></svg>
                <ul class="options">
                    <li><span>Salvar a biblioteca</span></li>
                    <li><span>Adicionar a fila</span></li>
                    <li><span>Adicionar a playlist</span></li>
                    <li><span>Remover dessa playlist</span></li>
                    <li><span>Desabilitar nessa playlist</span></li>
                </ul>
            </td>
        </tr>
      </tbody>
    </table>
    
<style>
table{
    width: 100%;
    table-layout: fixed;
}

table tbody tr td:nth-child(7) {
    width: 50px;
}

tr td:last-child {
    overflow: visible;
    position: relative;
}

table tbody tr td {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

table tbody tr td {
    font-size: 16px;
    text-align: left;
    vertical-align: middle;
    padding: 10px 15px;
    color: #bbb;
}

.options{
    border: 1px solid #3D4466;
    border-radius: 14px;
    margin: 10px 0 0 0;
    box-shadow: 0 6px 12px 0px rgba(0,0,0,0.16);
    transition: .5s opacity;
    position: absolute;
    z-index: 2;
    min-width: 175px;
    background: #252A41;
    top: 0;
    left: inherit;
    right: 100%;
    bottom: inherit;
 }
</style>

打印屏幕

You can use您可以使用

.options:not(:nth-last-child(-n+2)) {
    //style here
}

And set for them different styles with proper vertical align并通过适当的垂直对齐为他们设置不同的样式

You can try this CSS:你可以试试这个 CSS:

tr:nth-last-child(-n+2){/*This will select last two rows*/
    /*creates a context*/
    position: relative;
}
tr:nth-last-child(-n+2) td:last-child .option{/*this will select the drop down of last two rows*/
    /*positioned within tr*/
    position: absolute;
    bottom: 0;
}

Here you can replace 2 with 3 to grab last 3 elements.在这里,您可以将2替换为3以获取最后 3 个元素。

Whats going on?这是怎么回事?

Actually these selectors work like a loop which runs through all the elements.实际上,这些选择器的工作方式就像一个循环,贯穿所有元素。 Here n specifies that I want all the elements.这里n指定我想要所有元素。 The formula -n+2 will work like this:公式-n+2将像这样工作:

suppose we have 10 elements假设我们有 10 个元素

n = {0,1,2,3...,9} n = {0,1,2,3...,9}

-n+2 = 2,1,0,-1,-2,... -n+2 = 2,1,0,-1,-2,...

for each value of n.对于每个 n 值。

Negative values are disregarded.负值被忽略。 2 is the second last and 1 is the last element, 0 is also disregarded. 2 是倒数第二,1 是最后一个元素,0 也被忽略。

Answering my own question, I got a solution that worked for me using JS, and maybe is gonna be useful for someone in the future... So basically I checked if the element is overflowing his parent by getting the top and height of the element and of his parent.回答我自己的问题,我得到了一个使用 JS 对我有用的解决方案,也许将来会对某人有用......所以基本上我通过获取元素的顶部和高度来检查元素是否溢出他的父元素和他的父母。 I sum his height with his top, and I get the axis Y of the bottom of this element.我将他的高度与他的顶部相加,我得到了这个元素底部的 Y 轴。 If the axis Y of this element is greater than the axis Y of his parent, the element its overflowing.如果此元素的 Y 轴大于其父元素的 Y 轴,则该元素溢出。 To solve that just position the parent as relative and add a bottom: 0 on the child element.要解决这个问题,只需将父元素定位为相对并在子元素上添加一个底部:0。

const ulBottom = ul.getBoundingClientRect().top + ul.offsetHeight
const sectionBottom = section.getBoundingClientRect().top + section.offsetHeight
if(ulBottom > sectionBottom) ul.style.bottom = '0px'

(Sorry for any writing mistake, english isn't my first language) (抱歉任何写作错误,英语不是我的第一语言)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 Bootstrap 下拉菜单尊重移动设备上的水平视口限制? - How to make Bootstrap dropdown respect horizontal viewport limits on mobile? 如何使视口垂直居中? - How to make a vertically center of viewport? 关于视口的元素位置 - Element position with respect to viewport 如何在垂直方向和水平方向绝对固定位置以进行划分 - how to fix position vertically and absolute horizontally for a division 如何修复相对父亲的绝对溢出元素的位置? - How to fix the position of an absolute overflowing element with respect to a relative parent? Highcharts:如何使工具提示位置在视口中可见 - Highcharts: How to make Tooltip position to be visible in the Viewport 如何使基础操纵杆的尖端位置绝对? - How to make tip position absolute for foundation joyride? 如何垂直固定覆盖从视口顶部偏移的容器的固定位置背景图像的中心? - How do I vertically center a fixed position background image that covers a container that is offset from the top of the viewport? Javascript:查找div相对于视口的位置,与滚动无关 - Javascript: Find position of div with respect to viewport irrespective of scroll 如何使div覆盖没有固定位置的浏览器视口? - How to make div to overlay browser viewport without fixed position?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM