gpt4 book ai didi

html - CSS:将元素定位在区域之外而不更改滚动条

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:01 36 4
gpt4 key购买 nike

我有一个模态对话框,当模态内容太大而无法包含时,它会出现一个垂直滚动条。

我的问题是:当我想要“弹出”模式的控件(实际上是 Twitter Typeahead https://twitter.github.io/typeahead.js/)出现时,它会导致模式上的滚动条扩展。

可能最好通过示例来解释:

期望的行为

https://jsfiddle.net/0eara5xc/14/显示我想要的行为(在输入框中输入字母“a”),但是当常规内容太大(我需要)时,模式不会滚动。

不良行为

一旦我使模态可滚动,弹出窗口就会导致滚动条增加。

.modal-body {
max-height: 40vh;
overflow: hidden;
overflow-y: auto;
}

https://jsfiddle.net/0eara5xc/13/ (向下滚动并在输入框中输入字母“a”)
根据第一个示例,我实际上希望弹出窗口在模式之外流动。

Twitter Typeahead 控件创建自己的 position:relative 和 position:absolute 元素,所以我认为我不能使用 CSS 来绝对定位动态创建的弹出元素。

最佳答案

这里的问题是你想要有两件事是矛盾的。我将用几个例子来解释,直到我达到我的解决方案的想法。

如果我们采用流入元素(容器和内部元素),我们有两种情况:

(1) 内部元素的高度小于或等于 到容器的高度。在这种情况下,所有溢出( autoscrollhidden )都是相同的。

(2) 内部元素的高度更大 比容器的高度,在这种情况下,我们:

  • 保持默认行为,我们将有一个 溢出 我们可以元素 .


  • .el {
    border:5px solid green;
    height:100px;
    }
    .el > div {
    height:200px;
    width:100%;
    background:red;
    }
    <div class="el">
    <div>

    </div>
    </div>


  • 使用overflow:hidden隐藏一些内部元素所以我们看不到 溢出的部分。


  • .el {
    border:5px solid green;
    height:100px;
    overflow:hidden;
    }
    .el > div {
    height:200px;
    width:100%;
    background:red;
    }
    <div class="el">
    <div>

    </div>
    </div>


  • 使用overflow:scroll隐藏一些内部元素,我们只能看到溢出部分滚动 .


  • .el {
    border:5px solid green;
    height:100px;
    overflow:scroll;
    }
    .el > div {
    height:200px;
    width:100%;
    background:red;
    }
    <div class="el">
    <div>

    </div>
    </div>


    所以从技术上讲,我们不能让元素溢出滚动。

    使用 absolute:position 时几乎在所有情况下(除非我们为容器定义大高度)我们都会处于情况(2),因为绝对定位元素不在流中并且是 容器高度计算中未考虑 所以从逻辑上讲,内部元素的高度会更大,并且上面的相同逻辑将适用:

    .el {
    border:5px solid green;
    position:relative;
    }
    .el > div {
    height:100px;
    background:red;
    }
    .el > div:last-child {
    position:absolute;
    background:blue;
    right:0;
    left:0;
    }
    <div class="el">
    <div>
    we see the element
    </div>
    <div>

    </div>
    </div>

    <div class="el" style="overflow:hidden;margin-top:100px;">
    <div>
    we don't see the element
    </div>
    <div>

    </div>
    </div>

    <div class="el" style="overflow:scroll;">
    <div>
    we see the element element on scroll
    </div>
    <div>

    </div>
    </div>


    即使 overflow 也适用相同的逻辑。属性设置为绝对定位元素的另一个元素祖先。如您所见 here :

    In most cases, overflow can be computed for any box from the bounds and properties of that box itself, plus the overflow of each of its children.



    所以使用 overflow:scroll当任何子元素的高度增加或溢出时,滚动总是会增加,并且没有办法在外面有任何溢出的元素,因为这是默认行为,您可以通过设置 overflow:scroll 来更改它.

    解决方案

    避免将滚动应用于元素的第一个简单解决方案是使元素相对于应用滚动的元素的祖先定位。换句话说:与 overflow:scroll 的元素之间和绝对定位元素你不应该找到任何定位元素。正如您在这个复杂的声明中所读到的 here :

    ... a box may overflow, meaning its content lies partly or entirely outside of the box, e.g.:

    [...]

    A descendant box is positioned absolutely, partly outside the box. Such boxes are not always clipped by the overflow property on their ancestors; specifically, they are not clipped by the overflow of any ancestor between themselves and their containing block



    这个解决方案当然会破坏你的代码。

    另一个想法是使元素 position:fixed .像这样,它将相对于浏览器窗口,我们可以在滚动之外看到它,但是很难使用 CSS 调整它的位置。这种情况下我们可以考虑一些JS来动态调整元素的位置:
    $('.typeahead').on('keypress change',function(e){
    var y = $(this).offset().top + $(this).height();
    var x = $(this).offset().left;
    $('.tt-menu').css('top',y);
    $('.tt-menu').css('left',x);
    })

    如您所见,我们只是根据输入的位置计算元素的顶部/左侧位置。

    这是完整的代码:

    $(function () {
    $('.typeahead').on('keypress change',function(e){
    var y = $(this).offset().top + $(this).height()+5;
    var x = $(this).offset().left;
    $('.tt-menu').css('top',y);
    $('.tt-menu').css('left',x);
    })



    $('#btnPopup').on('click', function(){
    $('#divPopup').toggle();
    });

    var substringMatcher = function(strs) {

    return function findMatches(q, cb) {

    var matches, substringRegex;



    // an array that will be populated with substring matches

    matches = [];



    // regex used to determine if a string contains the substring `q`

    substrRegex = new RegExp(q, 'i');



    // iterate through the pool of strings and for any string that

    // contains the substring `q`, add it to the `matches` array

    $.each(strs, function(i, str) {

    if (substrRegex.test(str)) {

    matches.push(str);

    }

    });



    cb(matches);

    };

    };



    var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',

    'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',

    'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',

    'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',

    'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',

    'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',

    'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',

    'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',

    'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'

    ];



    $('.typeahead').typeahead({

    hint: true,

    highlight: true,

    minLength: 1

    },

    {

    name: 'states',

    source: substringMatcher(states)

    });

    });
    .modal-body {

    max-height: 40vh;
    overflow: hidden;
    overflow-y: auto;
    }

    .modal-header, .modal-footer {
    background-color: black;
    color: white;
    }

    #divSpacer {
    height: 40vh;
    background-color: rgb(220, 220, 220);
    }

    .tt-menu {
    background-color: red;
    position:fixed!important;
    }
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/result-light.css">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
    <div>
    <div class="modal-lg">
    <div class="modal-header">
    My modal title
    </div>
    <div class="modal-body">
    <div id="divSpacer">
    Lots of space taken up here...
    </div>

    <input class="typeahead" type="text" placeholder="States of USA">

    </div>
    <div class="modal-footer">
    </div>
    </div>
    </div>

    关于html - CSS:将元素定位在区域之外而不更改滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859408/

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