모달 바디에만 스크롤 막대를 두는 방법은 무엇입니까?


165

다음과 같은 요소가 있습니다.

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog" style="overflow-y: scroll; max-height:85%;  margin-top: 50px; margin-bottom:50px;" > 
        <div class="modal-content"> 
            <div class="modal-header"> 
                <h3 class="modal-title"></h3> 
            </div> 
            <div class="modal-body"></div> 
            <div class="modal-footer"></div> 
        </div> 
    </div> 
</div> 

그것은 모달 대화 상자를 다음과 같이 보여줍니다. 기본적으로 스크롤 막대를 전체 주위에 놓고 표시하려는 내용이 포함되어 modal-dialog있지 않습니다 modal-body.

이미지는 다음과 같습니다.

여기에 이미지 설명을 입력하십시오

스크롤 막대 modal-body만 어떻게 구합니까?

할당 style="overflow-y: scroll; max-height:85%; margin-top: 50px; margin-bottom:50px;"을 시도했지만 modal-body작동하지 않았습니다.


1
더 나은 도움을주기 위해 실제 샘플을 제공 하십시오. bootply.com/new 를 사용하여 도움을 받으실 수 있습니다 .
Carlos Calla

다음으로, 모달 내부에 선택적 유체 머리글 및 바닥 글 블록을 허용하여 본문 만 스크롤되도록하십시오. stackoverflow.com/questions/49173969/…
leontalbot

답변:


298

당신은 설정해야 height의를 .modal-body에 그것을 제공 overflow-y: auto. .modal-dialog오버플로 값 도로 재설정하십시오 initial.

작업 샘플을 참조하십시오.

http://www.bootply.com/T0yF2ZNTUd

.modal{
    display: block !important; /* I added this to see the modal, you don't need this */
}

/* Important part */
.modal-dialog{
    overflow-y: initial !important
}
.modal-body{
    height: 250px;
    overflow-y: auto;
}

고마워 잘 작동합니다. CSS에서 나에게 분명하지 않은 것이있을 수 있습니다. 작동하지 않는 모달 바디 최대 높이 : 65 %를 할당하려고했습니다. 제공 한 솔루션은 고정 높이를 모달 바디에 할당 한 경우에만 작동합니다. 모달 대화 상자가 창을 85 % 차지하고 모달 본문이 모달 대화 상자의 90 %를 차지해야한다고 말할 방법이 없습니까? 그러나 다시 한 번 당신이 제공 한 솔루션은 저에게 효과적입니다. 감사!
Subodh Nijsure

3
@SubodhNijsure 늦은 답변에 대해 죄송합니다. 부모의 %를 의미하기 때문에 높이를 %로 설정할 수 없습니다.이 경우 부모는 .modal-content이고 높이는 자식에 따라 다릅니다. 따라서 높이를 설정하지 않으면 작동하지 않습니다. .modal-content height 또는 .modal-body height를 설정하십시오. .modal-content height를 설정하면 .modal-body의 높이를 %로 설정할 수 있으며 부모의 %가되므로 100 %보다 작게 설정하면 빈 공간이 생깁니다. .modal-content의 높이를 모두 채우지 않기 때문입니다.
Carlos Calla

CSS의 initial키워드는 Internet Explorer에서는 작동하지 않지만 Edge에서는 작동합니다.
trysis

18
작은 포인터 : 가로 높이가 230px 이하인 일부 휴대 전화에서보기 흉하게 보일 수있는 "높이 250px"대신 가로 방향의 탐색 표시 줄이있는 경우 : max-height: 80vh;전체 뷰포트 높이의 80 %이며 최대 높이에만 사용하십시오. 작은 팝업입니다
Toskan

1
잘 작동하지만 모달을 닫은 후에는 '부모'창의 링크를 더 이상 클릭 할 수 없습니다 ... 다음 코드로 해결하십시오. .modal .modal-body {max-height : 420px; 오버플로 -y : 자동; }
cwhisperer

119

IE 9 이상 만 지원하는 경우이 CSS를 사용하여 창의 크기에 맞게 조정할 수 있습니다. 머리글 또는 바닥 글의 높이에 따라 "200px"를 조정해야 할 수도 있습니다.

.modal-body{
    max-height: calc(100vh - 200px);
    overflow-y: auto;
}

[1] 및 [2]에 따르면 Chrome, Firefox 및 Safari에서도 지원됩니다. [1] developer.mozilla.org/en-US/docs/Web/CSS/… [2] developer.mozilla.org/en-US/docs/Web/CSS/…
ctron

참고 :이 솔루션은 상단 또는 하단 중 선택된 옵션에 따라 팝 오버가 머리글 또는 바닥 글 아래에 표시됩니다. 내가 만든 바이올린을 참조하십시오 : jsfiddle.net/2qeo99k3/4 CSS 해킹이 없으면 그냥 바이올린에서 제거하면 볼 수 있습니다. 그것은 작동합니다
Marc Roussel

popover에 대한 해결 방법은 여기에서 찾을 수 있습니다 : stackoverflow.com/questions/45666828/…
Marc Roussel

37

@carlos calla와 @jonathan marston 결합 솔루션으로 문제가 해결되었습니다.

    /* Important part */
.modal-dialog{
    overflow-y: initial !important
}
.modal-body{
    max-height: calc(100vh - 200px);
    overflow-y: auto;
}

20

부트 스트랩 버전> = 4.3

Bootstrap 4.3은 새로운 내장 스크롤 기능을 모달에 추가했습니다. 이것은 컨텐츠의 크기가 페이지를 스크롤하는 경우 모달 본문 컨텐츠 만 스크롤하게합니다. 그것을 사용하려면, 단지 클래스 추가 모달 - 대화 - 스크롤 이 같은 사업부에 모달-대화 상자 클래스를.

예를 들면 다음과 같습니다.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalScrollable">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalScrollable" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalScrollableTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


14

에게 추가 카를로스 칼라 의 훌륭한 대답.

.modal-body의 높이를 설정해야하지만 미디어 쿼리를 사용하여 화면 크기에 적합한 지 확인할 수 있습니다.

.modal-body{
    height: 250px;
    overflow-y: auto;
}

@media (min-height: 500px) {
    .modal-body { height: 400px; }
}

@media (min-height: 800px) {
    .modal-body { height: 600px; }
}

휴대 기기에서 높이가 400px 이상이라고 가정하면 문제가 있습니다. 너비를 사용하여 높이를 결정하는 것은 실현 가능한 해결책이 아닙니다. 이 문제를 야기 할 수있는 사용 사례가 너무 많습니다.
딸기

2
@Strawberry 그는 실제로 최소 높이 (최소 너비 아님)를 사용했으며 주어진 높이가 미디어 쿼리의 최소 높이보다 작은 지 확인했습니다. 또한 미디어 쿼리는 OP가 달성하려는 목표에 따라 크게 달라질 수 있기 때문에 자신의 아이디어에 대한 예를 보여줄뿐입니다.
Carlos Calla

12

옵션 : 모달이 창 높이를 초과하지 않고 .modal-body에서 스크롤 막대를 사용하지 않으려면이 반응 형 솔루션을 사용할 수 있습니다. 실제 데모를 참조하십시오 : http://codepen.io/dimbslmh/full/mKfCc/

function setModalMaxHeight(element) {
  this.$element     = $(element);
  this.$content     = this.$element.find('.modal-content');
  var borderWidth   = this.$content.outerHeight() - this.$content.innerHeight();
  var dialogMargin  = $(window).width() > 767 ? 60 : 20;
  var contentHeight = $(window).height() - (dialogMargin + borderWidth);
  var headerHeight  = this.$element.find('.modal-header').outerHeight() || 0;
  var footerHeight  = this.$element.find('.modal-footer').outerHeight() || 0;
  var maxHeight     = contentHeight - (headerHeight + footerHeight);

  this.$content.css({
      'overflow': 'hidden'
  });

  this.$element
    .find('.modal-body').css({
      'max-height': maxHeight,
      'overflow-y': 'auto'
  });
}

$('.modal').on('show.bs.modal', function() {
  $(this).show();
  setModalMaxHeight(this);
});

$(window).resize(function() {
  if ($('.modal.in').length != 0) {
    setModalMaxHeight($('.modal.in'));
  }
});

5

나는 이것이 오래된 주제라는 것을 알고 있지만 이것은 다른 누군가를 도울 수 있습니다.

모달 대화 상자 요소 위치를 고정시켜 본문을 스크롤 할 수있었습니다. 그리고 브라우저 창의 정확한 높이를 알 수 없으므로 헤더와 바닥 글의 높이, 확실했던 정보를 가져 왔습니다. 그런 다음 모달 바디 요소의 위쪽 및 아래쪽 여백을 해당 높이와 일치시킬 수있었습니다. 그런 다음 내가 찾던 결과를 얻었습니다. 나는 일을 보여주기 위해 바이올린을 함께 던졌다.

또한 전체 화면 대화 상자를 원하면 width : auto의 주석 처리를 제거하십시오. .modal-dialog.full-screen 섹션 내부.

https://jsfiddle.net/lot224/znrktLej/

그리고 여기 부트 스트랩 대화 상자를 수정하는 데 사용한 CSS가 있습니다.

.modal-dialog.full-screen {
    position:fixed;
    //width:auto;  // uncomment to make the width based on the left/right attributes.
    margin:auto;                        
    left:0px;
    right:0px;
    top:0px;
    bottom:0px;
}

.modal-dialog.full-screen .modal-content {
      position:absolute;
      left:10px;
      right:10px;
      top:10px;
      bottom:10px;
}

.modal-dialog.full-screen .modal-content .modal-header {
        height:55px;  // adjust as needed.
}

.modal-dialog.full-screen .modal-content .modal-body {
        overflow-y: auto;
          position: absolute;
          top: 0;
          bottom: 0;
        left:0;
        right:0;
          margin-top: 55px; // .modal-header height
          margin-bottom: 80px;  // .modal-footer height
}

.modal-dialog.full-screen .modal-content .modal-footer {
        height:80px;  // adjust as needed.
        position:absolute;
        bottom:0;
        left:0;
        right:0;
}

5

또는 더 간단하게 태그 사이에 CSS 클래스를 넣을 수 있습니다.

<div style="height: 35px;overflow-y: auto;"> Some text o othre div scroll </div>

1
#scroll-wrap {
    max-height: 50vh;
    overflow-y: auto;
}

사용 max-heightvh상의 단위로 modal-body또는의 래퍼 DIV의 내부 modal-body. 이것은 modal-body사용자가 창의 크기를 조정할 때 자동으로 래핑 div (이 예제에서)의 크기를 조정합니다.

vh 뷰포트 높이에 대한 뷰포트 크기의 1 %를 나타내는 길이 단위입니다.

vh단위 별 브라우저 호환성 차트 .

예 : https://jsfiddle.net/q3xwr53f/


1

나를 위해 일한 것은 높이를 100 %로 설정하는 것입니다. 자동 희망에 오버플로가 있으면 도움이 될 것입니다.

   <div style="height: 100%;overflow-y: auto;"> Some text o othre div scroll </div>

0

몸의 높이에 비례하여 모달 높이를 설정하는 간단한 js 솔루션 :

$(document).ready(function () {
    $('head').append('<style type="text/css">.modal .modal-body {max-height: ' + ($('body').height() * .8) + 'px;overflow-y: auto;}.modal-open .modal{overflow-y: hidden !important;}</style>');
});

몸의 키는 100 %가되어야합니다 :

html, body {
    height: 100%;
    min-height: 100%;
}

모달 바디 높이를 바디의 80 %로 설정했는데, 물론 커스터마이징이 가능합니다.

도움이 되길 바랍니다.


당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.