JavaScript: Hide scrollbar while page loading

Viewed 122

I have a preloader where it will show when the page is loading, it will disappear after the page is loaded plus one second.

What I want to achieve is that I don't want people to be able to see the scrollbar while the preloader is showing. How do I do it?

var loader = document.getElementById("container");
window.addEventListener("load", function() {
  window.setTimeout(() => {
    loader.style.display = "none";
  }, 1000)
})
#container {
  background-color: #ddd;
  height: 100vh;
}

P {
  height: 100vh;
  overflow: visible;
}

.loader1{
    position: relative;
    height: 80px;
    width: 80px;
    border-radius: 80px;
    border: 3px solid  rgba(255,255,255, .7);
    top: 28%;
    top: calc(50% - 43px);
    left: 35%;
    left: calc(50% - 43px);
    -webkit-transform-origin: 50% 50%; transform-origin: 50% 50%;
    -webkit-animation: loader1 3s linear infinite; animation: loader1 3s linear infinite;
}

.loader1:after{
    content: "";
    position: absolute;
    top: -5px;
    left: 20px;
    width: 11px;
    height: 11px;
    border-radius: 10px;
    background-color: #fff;
}

@-webkit-keyframes loader1{
    0%{-webkit-transform:rotate(0deg);}
    100%{-webkit-transform:rotate(360deg);}
}

@keyframes loader1{
    0%{transform:rotate(0deg);}
    100%{transform:rotate(360deg);}
}
<div id="container">
  <div class="loader1"></div>
</div>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

1 Answers

You can do it like below:

window.addEventListener("load", function() {
  document.body.style.overflow = "hidden"
  window.setTimeout(() => {
    loader.style.display = "none";
    document.body.style.overflow = "auto"
  }, 1000)
})
Related