javascript - 实现一个完整的页面动画加载器

标签 javascript html css

我创建了一个 ripple 整页加载器。我正在尝试创建类似 here 的. 我希望代码简单,理想情况下没有插件。 我可以加载页面预加载器,但想知道如何在主页加载后让加载页面消失?页面不会褪色!研究并尝试了各种方法。 这是到目前为止的代码:

// Page loading animation
$(window).on('load', function() {
  if ($('.cover').length) {
    $('.cover').parallax({
      imageSrc: $('.cover').data('image'),
      zIndex: '1'
    });
  }

  $("#preloader").animate({
    'opacity': '0'
  }, 600, function() {
    setTimeout(function() {
      $("#preloader").css("visibility", "hidden").fadeOut();
    }, 300);
  });
});
#preloader {
  overflow: hidden;
  background-color: #fb5849;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  position: fixed;
  z-index: 99999;
  color: #fff;
}

#preloader .jumper {
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: block;
  position: absolute;
  margin: auto;
  width: 50px;
  height: 50px;
}

#preloader .jumper>div {
  background-color: #fff;
  width: 10px;
  height: 10px;
  border-radius: 100%;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  position: absolute;
  opacity: 0;
  width: 50px;
  height: 50px;
  -webkit-animation: jumper 1s 0s linear infinite;
  animation: jumper 1s 0s linear infinite;
}

#preloader .jumper>div:nth-child(2) {
  -webkit-animation-delay: 0.33333s;
  animation-delay: 0.33333s;
}

#preloader .jumper>div:nth-child(3) {
  -webkit-animation-delay: 0.66666s;
  animation-delay: 0.66666s;
}

@-webkit-keyframes jumper {
  0% {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
  }
  5% {
    opacity: 1;
  }
  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 0;
  }
}

@keyframes jumper {
  0% {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
  }
  5% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
<div id="preloader">
  <div class="jumper">
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

最佳答案

您可以使用 vanilla JavaScript 如下所示进行操作。您可以将其改编为 jQuery。您也可以删除仅为测试目的添加的 setTimout

window.addEventListener("load", () => {
  setTimeout(() => {
    document.getElementById("preloader").classList.add("hide");
  }, 1000);
});
#preloader {
  overflow: hidden;
  background-color: #fb5849;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  position: fixed;
  z-index: 99999;
  color: #fff;
  opacity: 1;
  transition: opacity 1s;
}

#preloader.hide {
  opacity: 0;
  pointer-events: none;
}

#preloader .jumper {
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: block;
  position: absolute;
  margin: auto;
  width: 50px;
  height: 50px;
}

#preloader .jumper > div {
  background-color: #fff;
  width: 10px;
  height: 10px;
  border-radius: 100%;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  position: absolute;
  opacity: 0;
  width: 50px;
  height: 50px;
  -webkit-animation: jumper 1s 0s linear infinite;
  animation: jumper 1s 0s linear infinite;
}

#preloader .jumper > div:nth-child(2) {
  -webkit-animation-delay: 0.33333s;
  animation-delay: 0.33333s;
}

#preloader .jumper > div:nth-child(3) {
  -webkit-animation-delay: 0.66666s;
  animation-delay: 0.66666s;
}

@-webkit-keyframes jumper {
  0% {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
  }
  5% {
    opacity: 1;
  }
  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 0;
  }
}

@keyframes jumper {
  0% {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
  }
  5% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
<div id="preloader">
  <div class="jumper">
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

关于javascript - 实现一个完整的页面动画加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72309155/

相关文章:

javascript - 使用纯 JavaScript 更改导航列表中的选定列表元素

html - 在菜单 li 中将文本居中对齐

css - 为什么此文本在 Webkit 中弹出到左侧?

javascript - AngularJS 变量插入到另一个不是复制值

javascript - 选择一个以特定 div 内的内容结尾的 id

JavaScript 不适用于 @Html.CheckBoxFor

jquery - 如何清晰地定义 $().on() 选择器

html - 无法在 Wordpress 中使用字体系列更改字体

javascript - cloudfoundry 上的 JSDOM 抓取不起作用

javascript - 谷歌登录时出现“用户关闭弹出窗口”错误