首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模式中设置div的高度以使其滚动?

如何在模式中设置div的高度以使其滚动?
EN

Stack Overflow用户
提问于 2021-11-07 13:53:37
回答 1查看 75关注 0票数 0

我有一个结构如下的模态:

代码语言:javascript
复制
  <div class="modal fade" id="editSaga" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg modal-dialog-scrollable">
    <div class="modal-content">
        <div class="modal-body row">
            <div class="col-7 firstchild">
                <div class="firstchild-header">
                </div>
                <div class="firstchild-body1">
                    // some content
                </div>
                <div class="firstchild-body2">
                    // some other content
                </div>
            </div>
            <div class="col-5">
            </div>
        <div>
    </div>
</div>

firstchild-body2的内容超出父class col-7的内容时,我想让它滚动,样式如下:

代码语言:javascript
复制
.col-7 {
    max-height: 100%;
    overflow: hidden;
}

我尝试使用jquery并以这种方式获取其高度:

代码语言:javascript
复制
$(".firstchild-body2").height(`$("#editSaga .modal-content").height()` - $(".firstchild- 
 header").outerHeight() - $(".firstchild-body1").outerHeight());

但我没有工作,因为我总是包含溢出元素的高度。然后我将$("#editSaga .modal-content").height()更改为$(".col-7").height()。但是这个解决方案也根本不起作用。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-07 14:43:21

我在这里为您制作了一个有效的代码示例:https://codepen.io/solutionsswift/pen/GRvxLOd

要在第一个子体2中应用正确的滚动,需要将其高度设置为auto。此外,您还需要设置col-7的最大高度。

使用JS,您可以在第一个can body2元素上设置max-height值,这样它就可以自动应用滚动条了。

不要忘记在body2上设置overflow: auto!

代码语言:javascript
复制
const header = document.querySelector('.firstchild-header');
const body1 = document.querySelector('.firstchild-body1');
const body2 = document.querySelector('.firstchild-body2');
const col7 = document.querySelector('.col-7');

// Set the max-height = total height of col-7 minus the height of header and 

body2.style.maxHeight = col7.offsetHeight - header.offsetHeight - body1.offsetHeight + 'px';
代码语言:javascript
复制
.modal-body {
    height: 100%;
    max-height: 80px;
}

   
.col-7 {
  overflow: hidden;
  width: 100px;
  height: auto; 
  background-color: red;
}

.firstchild-body2 {
  box-sizing: border-box;
  overflow-y: auto;
  height: auto;
}
代码语言:javascript
复制
<div class="modal-dialog modal-lg modal-dialog-scrollable">
    <div class="modal-content">
        <div class="modal-body row">
            <div class="col-7 firstchild">
                <div class="firstchild-header">
                  header
                </div>
                <div class="firstchild-body1">
                    Body 1
                </div>
                <div class="firstchild-body2">
                    Body 2 sdfsdf sfsdf ssdf sf sd fsd fs fs fs fs 
                </div>
            </div>
            <div class="col-5">
            </div>
        <div>
    </div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69873050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档