【问题标题】:Add scrollbar to bootstrap modal body将滚动条添加到引导模态体
【发布时间】:2022-01-25 17:36:35
【问题描述】:

我正在尝试将滚动条添加到引导模式主体。但是它会添加到整个内容中。如何仅将其添加到正文中。使用的是具有长内容的模态框,当达到给定的最大高度时,模态框会自动滚动。

这是我的代码

<head>
<!-- Bootstrap CSS-->
<link href="/<s:text name="app.name"/>/css/bootstrap.min.css" rel="stylesheet" type="text/css">

.modal-body {
max-height: calc(100vh - 212px);
overflow-y: auto;
}
</head>


<body>
<div>
<s:if test="user.level == 0 && name != ''">
<button id="roleBtn" class="btn btn-info" type="button" onclick=""
data-toggle="modal" data-target="#roleModal">
<s:text name="roles"/>
</button>
</s:if>
</div>

<div class="modal" id="roleModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="overflow-y: scroll; max-height:80%;  margin-top: 60px; margin-bottom:30px;">
<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" id="myModalLabel"><s:text name="headingRoles"/></h4>
</div>

<s:form theme="simple" method="post" action="UserSupport">
<div class="modal-body">
<div class="container-fluid">
<s:hidden name="docType" value="userLevel"/>
<div>
<s:hidden name="name"/>
<s:iterator value="userRoles" status="rowstatus">
<s:hidden name="userRoles[%{#rowstatus.index}].role" value="%{role}"/>
<div class="form-group col-xs-12">
<div class="col-xs-4"></div>

<div class="col-xs-8" align="left">
<div class="checkbox">
<label></label><s:checkbox name="userRoles[%{#rowstatus.index}].selected"
value="name != ''"/> <s:property
value="roleName"/></label>
</div>
</div>
</div>
</s:iterator>
</div>
</div>
</div>

<div class="modal-footer">
<div class="panel-body" align="center">
<s:submit method="saveRoles" cssClass="btn btn-primary"
value="%{getText('save')}"></s:submit>
<button type="button" method="resetRoles" class="btn btn-default" data-dismiss="modal">
<s:text name="close"/>
</button>
</div>
</div>
</s:form>
</div>
</div>
</div>
</body>

它没有按预期工作。模态窗口确实达到了最大尺寸,但它只是在那里剪切其内容,甚至不显示页脚。欢迎回答

谢谢

【问题讨论】:

  • 如果你为它准备一个小提琴,其他人会更容易检查/玩你的代码。
  • 看到这个:bootply.com/T0yF2ZNTUd

标签: twitter-bootstrap-3


【解决方案1】:

只需向 .modal 类提供溢出隐藏,并为模态体提供溢出滚动...

.modal .modal-content{
overflow:hidden;
}
.modal-body{
overflow-y:scroll; // to get scrollbar only for y axis
}

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    <head>
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
    body,html{
            width:100%;
            height:100%;
            padding:0px;
    margin:0px;
    }
    .modal-dialog{
            height:calc(100% - 60px);
    }
    .modal-content{
      height:100%;
     }
    .modal-header{
            height:50px;
    
    }
    .model-footer{
            height:75px;
    }
    .modal-body {
    height:calc(100% - 125px);
    overflow-y: scroll;     /*give auto it will take based in content */
    }
    </style>
    </head>
    
    
    <body>
    <div>
    <s:if test="user.level == 0 && name != ''">
    <button id="roleBtn" class="btn btn-info" type="button" onclick=""
    data-toggle="modal" data-target="#roleModal">Open model
    <s:text name="roles"/>
    </button>
    </s:if>
    </div>
    
    <div class="modal" id="roleModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="">
    <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" id="myModalLabel"><s:text name="headingRoles"/></h4>
    </div>
    
    <s:form theme="simple" method="post" action="UserSupport">
    <div class="modal-body">
    <div class="container-fluid">
    <s:hidden name="docType" value="userLevel"/>
    <div>
    <s:hidden name="name"/>
    <s:iterator value="userRoles" status="rowstatus">
    <s:hidden name="userRoles[%{#rowstatus.index}].role" value="%{role}"/>
    <div class="form-group col-xs-12">
    <div class="col-xs-4"></div>
    
    <div class="col-xs-8" align="left">
    <div class="checkbox">
    <label></label><s:checkbox name="userRoles[%{#rowstatus.index}].selected"
    value="name != ''"/> <s:property
    value="roleName"/></label>
    </div>
    </div>
    </div>
    </s:iterator>
    </div>
    </div>
    </div>
    
    <div class="modal-footer">
    <div class="panel-body" align="center">
    <s:submit method="saveRoles" cssClass="btn btn-primary"
    value="%{getText('save')}"></s:submit>
    <button type="button" method="resetRoles" class="btn btn-default" data-dismiss="modal">
    <s:text name="close"/>
    </button>
    </div>
    </div>
    </s:form>
    </div>
    </div>
    </div>
    </body>

    【讨论】:

      【解决方案3】:

      https://getbootstrap.com/docs/4.5/components/modal/#exampleModalLongTitle

      <!-- Scrollable modal -->
      <div class="modal-dialog modal-dialog-scrollable">
        ...
      </div>
      

      【讨论】:

        【解决方案4】:

        如果你使用 Bootstrap,不要添加一些 CSS,只需在带有 modal-content 类的 div 上使用类“overflow-hidden”。

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多