How can I show CSS placeholder skeletons and hide them when content or page is fully loaded

Viewed 38

I have CSS placeholders/skeleton animations and some content in PHP and HTML. I want to show these placeholders when the page is initially opened (even before it finishes loading) and hide them when the page is fully loaded to show the content to show the content.

My html content include carousels and cards that hold data from the database.

.placeholder {
  display: inline-block;
  min-height: 1em;
  vertical-align: middle;
  cursor: wait;
  background-color: currentColor;
  opacity: .5;
  border-radius: 50px;
}

.placeholder.btn::before {
  display: inline-block;
  content: ""
}

.placeholder-xs {
  min-height: .6em
}

.placeholder-sm {
  min-height: .8em
}

.placeholder-lg {
  min-height: 1.2em
}

.placeholder-xl {
  min-height: 6rem;
  border-radius: 6px !important;
}

.placeholder-ml {
  min-height: 4rem;
  border-radius: 6px !important;
}

.placeholder-glow .placeholder {
  -webkit-animation: placeholder-glow 2s ease-in-out infinite;
  animation: placeholder-glow 2s ease-in-out infinite
}

@-webkit-keyframes placeholder-glow {
  50% {
    opacity: .2
  }
}

@keyframes placeholder-glow {
  50% {
    opacity: .2
  }
}

.placeholder-wave {
  -webkit-mask-image: linear-gradient(130deg, #000 55%, rgba(0, 0, 0, 0.8) 75%, #000 95%);
  mask-image: linear-gradient(130deg, #000 55%, rgba(0, 0, 0, 0.8) 75%, #000 95%);
  -webkit-mask-size: 200% 100%;
  mask-size: 200% 100%;
  -webkit-animation: placeholder-wave 2s linear infinite;
  animation: placeholder-wave 2s linear infinite
}

@-webkit-keyframes placeholder-wave {
  100% {
    -webkit-mask-position: -200% 0%;
    mask-position: -200% 0%
  }
}

@keyframes placeholder-wave {
  100% {
    -webkit-mask-position: -200% 0%;
    mask-position: -200% 0%
  }
}
<div class="header-large-title">
  <h1 class="title">Hello
    <?php echo htmlspecialchars($_SESSION["lastname"]); ?>
  </h1>
  <?php
    $countfarm = mysqli_query($data, "SELECT count(farm_active) as total from farmerfruit where farm_active = '1' AND farmer_id='" . $_SESSION['id'] . "'");
    $totalcount = mysqli_fetch_assoc($countfarm);
  ?>
  <h4 class="subtitle">You have
    <a href="#" class="headerButton" data-toggle="modal" data-target="#ModalBasic">
      <?php echo $totalcount['total']; ?> Ongoing
    </a>
    Farm Activities
  </h4>
</div>
<div class="section full mt-3 mb-3 main-banner">
  <div class="carousel-multiple owl-carousel owl-theme">
    <div class="item">
      <div class="card">
        <a href="#" data-toggle="modal" data-target="#ModalBasic">
          <h4 class="head-card">My Farm</h4>
          <img src="assets/img/sample/photo/farm/1.png" class="card-img-top" alt="image">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="card">
        <a href="#">
          <h4 class="head-card">Training</h4>
          <img src="assets/img/sample/photo/farm/2.png" class="card-img-top" alt="image">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="card">
        <a href="shop.php">
          <h4 class="head-card">Our Shop</h4>
          <img src="assets/img/sample/photo/farm/3.png" class="card-img-top" alt="image">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="card">
        <a href="#">
          <h4 class="head-card">Trends</h4>
          <img src="assets/img/sample/photo/farm/4.png" class="card-img-top" alt="image">
        </a>
      </div>
    </div>
  </div>
</div>

Please help. Thank you

1 Answers

Here is a simple example. You can use window.onload to invoke a function when the page is loaded. Then you can add a class to <body> so that you will be able to change the CSS accordingly. I just added a one second delay for the demonstration as this is a very small DOM and loads quickly.

window.onload = function(){ 
  setTimeout(()=>{ // just for demo
    document.body.classList.add('loaded');
  }, 1000)
};
.container{
  width:50%;
  margin-bottom:10px
}

.placeholder {
  animation: skeleton 1s linear infinite alternate;
}
.placeholder .content{
  visibility: hidden
}

@keyframes skeleton {
  0% {
    background-color: hsl(200, 20%, 80%);
  }
  100% {
    background-color: hsl(200, 20%, 95%);
  }
}

body.loaded .placeholder{
  animation: none
}

body.loaded .placeholder .content{
  visibility: visible
}
<div class="container placeholder">
  <div class="content">
    I'm Loaded<br />Place holder is gone...<br />...
  </div>
</div>

<div class="container placeholder">
  <div class="content">I'm smaller</div>
</div>

Related