Background images jump when opening modal

Viewed 1164

I build a page with a parallax effect and an image gallery using Bootstrap 4. The problem is that the background image jumps/resizes when I open a modal and I feel like I tried everything to fix this. BS does a good job keeping all other elements where they are, scrollbar or not, but not the background image.

<

// call ekko-lightbox
$(document).on('click', '[data-toggle="lightbox"]', function(event) {
  event.preventDefault();
  $(this).ekkoLightbox();
});
body {
  position: relative;
  font-size: 100%;
}

body,
html,
h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: "Lato", sans-serif;
  line-height: 1.8;
}

body,
html {
  height: 100%;
  color: #666666;
}

.bgimg-1 {
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center center;
  background-color: black;
  background-image: url("https://cdnb.artstation.com/p/assets/images/images/005/540/665/large/thorsten-erdt-nami-small.jpg?1491824822");
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ekko-lightbox/5.3.0/ekko-lightbox.css">

<body>
  <header class="container-fluid h-100 bgimg-1" id="home">
    <div class="row h-100 justify-content-center align-items-center">
      <div class="col col-sm-auto text-white text-center">
        <h2 class="d-md-none">ART OF</h2>
        <h2 class="bg-dark d-block">THORSTEN ERDT</h2>
        <h2 class="d-none d-md-block">ILLUSTRATION</h2>
      </div>
    </div>
  </header>

  <section class="container" id="editorial">
    <div class="row justify-content-center">
      <a href="IMG" data-toggle="lightbox" data-gallery="img-gallery"><img src="thumbnail" alt="captiontext1" style="width:50px"></a>
    </div>
  </section>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ekko-lightbox/5.3.0/ekko-lightbox.js"></script>

If I set

background-size:{auto;}
background-position: {left top;}

it wont jump anymore, but it wont resize for smaller devices either, so that wont do.

If I set

overflow-y:{scroll;}

it still adds padding to everything which results in everything jumping...

body.modal-open {padding:0;}

fixes that, but now the navbar is the only thing jumping. Oh and of course I can scroll the page while the modal is open, too.

If someone has another idea how to fix this I am all ears.

1 Answers

When you open a bootstrap-modal, It will remove vertical scroll if you have, So you feel like your background image is jumping. And it is feature.

If you want to override it's css , you can add following code at the end of your css.

 .modal-open {
   overflow-y: inherit;  
 }

It is better to use overflow-y : inherit instead of using overflow-y : scroll as described below:

 .modal-open {
   overflow-y: scroll;  
 }

See it is working in Demo

Edit:

 .modal-open{
  padding:0 !important;
  overflow-y: auto;
 }

Add the above code in your css. and all your problem solved. For more details: Click me

Hope this solution will work for you.
If you have any doubts, comment in comment section.

Related