admin管理员组

文章数量:1294327

I'm sure this is easy fix. I need that my preloader fade out slowly. I tried with css animation but didn't work. Can somebody tell me how should i do that in javascript ? As you can see in example, the transition is very rough. I don't want that.

<script> <!--Preloader-->
var myVar;

function preloader() {
    myVar = setTimeout(showPage, 1500);
}

function showPage() {
  document.getElementById("preloader").style.display = "none";
  document.getElementById("wrapper").style.display = "block";
}
</script>

CODEPEN EXAMPLE

I'm sure this is easy fix. I need that my preloader fade out slowly. I tried with css animation but didn't work. Can somebody tell me how should i do that in javascript ? As you can see in example, the transition is very rough. I don't want that.

<script> <!--Preloader-->
var myVar;

function preloader() {
    myVar = setTimeout(showPage, 1500);
}

function showPage() {
  document.getElementById("preloader").style.display = "none";
  document.getElementById("wrapper").style.display = "block";
}
</script>

CODEPEN EXAMPLE

Share asked Jun 29, 2016 at 10:50 pHenomenpHenomen 1532 gold badges5 silver badges19 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

Add following changes into your codes.

#preloader {
  transition:1s ease;
}

#wrapper {
  opacity:0;/*Remove display and hide opacity*/
 }

function showPage() {
  document.getElementById("preloader").style.opacity = 0;
  document.getElementById("wrapper").style.opacity = 1;
}

transition doent work with display block and none..

use

var myVar;

function preloader() {
    myVar = setTimeout(showPage, 1500);
}

function showPage() {
  document.getElementById("preloader").style.opacity = 0;
  document.getElementById("wrapper").style.opacity = 1;
}

and

#preloader {
  position: absolute;
  z-index: 1000;
  background-color:black;
  width: 100vw;
  height: 100vh;
  color:white;
  transition: 0.5s all linear
}

You can't animate display: none itself, what you can do is animate opacity: 0 for example.

You'll add display: block, while opacity is still 0. After that add opacity: 1 and animate that

Try this example may helps you.

$(function() {
  $("#loader-image").fadeIn(500, function() {
    $("#loader-image").fadeOut(1000, function() {
      $(".loader-container").fadeOut(1000, function() {
        alert("loaded!");
      });
    });
  });
});
body {
  background-color: black;
}
.loader-container {
  background-color: yellow;
  height: 200px;
}
#loader-image {
  display: none;
  background-color: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<body>
  <div class="loader-container">
    <img src="image.png" alt="Image" id="loader-image" />
  </div>
</body>

You can use a CSS transition. Change your preloader styles to:

#preloader {
  position: absolute;
  z-index: 1000;
  background-color:black;
  width: 100vw;
  height: 100vh;
  color:white;
  display: block;
  opacity: 1; // Add opacity
  transition: 1s opacity ease-in; // Add transition
}

Add styles for the hidden class:

#preloader.hidden {
  opacity: 0;
}

Then when you call showPage()

function showPage() {
      // Add the newly defined hidden class to the preloader element
      document.getElementById("preloader").classList.add('hidden');
}

Here is a working example.

<style>
        #pre-loader {
            background-color: #fff;
            height: 100%;
            width: 100%;
            position: fixed;
            z-index: 1;
            margin-top: 0px;
            top: 0px;
            left: 0px;
            bottom: 0px;
            overflow: hidden !important;
            right: 0px;
            z-index: 999999;
        }

        #pre-loader img {
            text-align: center;
            left: 0;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            -webkit-transform: translateY(-50%);
            -o-transform: translateY(-50%);
            -ms-transform: translateY(-50%);
            -moz-transform: translateY(-50%);
            z-index: 99;
            margin: 0 auto;
        }

</style>
<body>
<div id="pre-loader">
    

<img src="https://www.google./urlsa=i&url=https%3A%2F%2Floading.io%2Fspinner%2Fspinner%2F&psig=AOvVaw2ZwZlaBvc7DYJD63k_Gzkx&ust=1724309409824000&source=images&cd=vfe&opi=89978449&ved=0CBAQjRxqFwoTCKDTjaG_hYgDFQAAAAAdAAAAABAE" alt="Loading..." />
    </div>
</body>

<script>
    function pre_loader() {
        $("#pre-loader").delay(0).fadeOut("slow");
     }
    pre_loader();
</script>

本文标签: javascriptPreloader fade out and content fade inStack Overflow