jquery - 在可滚动模态中具有固定位置的 Div

标签 jquery html css twitter-bootstrap

我在带有滚动的模式中有一个 div,它应该相对于浏览器窗口有固定的位置,并且不应该随着模式滚动。

我尝试了 position:fixed,但它不起作用。在这里您可以在其上下文中看到问题:

$('.launchConfirm').on('click', function (e) {
    $('#confirm').modal({
        show: true
    });
});
@import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css' );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="page-container">
    <div class="container">
        <br />
        <button type="button" class="btn launchConfirm">Open modal</button>
    </div>
</div>
<div class="modal fade" id="confirm">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                 <h4 class="modal-title">Modal title</h4>

            </div>
            <div class="modal-body">
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <p>One fine body&hellip;</p>
                <div style="position:fixed;top:40px;left:10px;background-color:red">This div should be fixed<br>and should not scroll away.</div>
            </div>
            <div class="modal-footer">
                some random buttons
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

我该如何修复那个 div?

最佳答案

您需要将 DIV 移出模态框。

当祖先元素应用了transform 时,固定定位与viewport 相关除外.modal-dialog 应用了 transform,它创建了一个新的上下文来放置 DIV(这就是为什么你的 DIV 留在模态内部)。

CSS Positioning - MDN

关于jquery - 在可滚动模态中具有固定位置的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41937320/

相关文章:

javascript - 基于 JavaScript/jQuery 中的格式字符串格式化数字

html - 将内联 block div 移动到其兄弟下方

css - Facebook 新 Page Plugin 自定义样式

javascript - react JS : Collapsible sidebar

jquery - 需要有关可折叠内容部分的帮助

javascript - 在 php foreach 循环中选择特定元素 jquery

javascript - jQuery 如何让事件每 x 秒发生一次?

javascript - jQuery 将自动完成结果放入文本区域

javascript - 如何用纯 JavaScript 查找具有已知类的父级

javascript - 从字符串中解析 JSON 数组