Scrollbar showing alone

1

Follow the code below:

HTML:

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<div id="myModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body" style="height: 300px">
        <div class="loader" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto;"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

CSS:

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

.loader {
  border: 16px solid #f3f3f3;
  /* Light grey */
  border-top: 16px solid #3498db;
  /* Blue */
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

If you prefer JSFiddle: link

Problem:

You're showing the scroll bar alone while loading does animation. How can I fix this?

    
asked by anonymous 03.10.2017 / 03:32

1 answer

3

You can hide overflow-y while loading is being displayed, when you hide the loading set the overflow-y as auto again.

$('#myModal .modal-body').on('scroll', function() {
    console.log('scroll y');
});
// oculta o overflow-y
$('#myModal .modal-body').css({
  	'overflow-y': 'hidden'
  });
/// Apenas para simular
setTimeout(function(){
  $(".loader").hide(); // Oculta o loading
  $('#myModal .modal-body').html('<p>Paragrafo</p><p>Paragrafo</p><p>Paragrafo</p><p>Paragrafo</p><p>Paragrafo</p><p>Paragrafo</p><p>Paragrafo</p>');
  // Define o overflow-y como auto
  $('#myModal .modal-body').css({
  	'overflow-y': 'auto'
  })
}, 5000);
.modal-body {
  max-height: calc(100vh - 210px);
  overflow-y: auto;
}

.loader {
  border: 16px solid #f3f3f3;
  /* Light grey */
  border-top: 16px solid #3498db;
  /* Blue */
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<div id="myModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body" style="height: 300px">
        <div class="loader" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto;"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
    
03.10.2017 / 03:54