怎么给网页加入加载过渡的动画?

36次阅读
2024年08月13日 14:19:21

共计 2717 个字符,预计需要花费 7 分钟才能阅读完成。

简单的来说,就是提前准备好一个加载动画的效果在页面上,利用 JS 判断页面是否加载完成,加载完成后隐藏或者移除动画即可。其实网站页面打开快的话是根本不需要的,除非是打开很慢的网页,设置这个动画能提高用户访问体验,也更加有逼格。在很多的个人博客上面见得比较多,企业网站基本没有使用的,我也试着去了解了一下。

HTML部分

<!--加载动画 start-->
<div id="loading-box">
  <div class="loading-left-bg"></div>
  <div class="loading-right-bg"></div>
  <div class="spinner-box">
    <div class="loader">
      <div class="inner one"></div>
      <div class="inner two"></div>
      <div class="inner three"></div>
    </div>
    <div class="loading-word">
      <p class="loading-title" id="loading-title">Yecssの主页</p>
      <span id="loading-text">Loading...</span>
    </div>
  </div>
</div>
<!--加载动画 end-->

‌CSS部分

#loading-box .loading-left-bg,
#loading-box .loading-right-bg {
  position: fixed;
  z-index: 999998;
  width: 50%;
  height: 100%;
  background-color: rgb(81 81 81 / 80%);
  transition: all 0.7s cubic-bezier(0.42, 0, 0, 1.01);
  backdrop-filter: blur(10px);
}
#loading-box .loading-right-bg {
  right: 0;
}
#loading-box > .spinner-box {
  position: fixed;
  z-index: 999999;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
}
#loading-box .spinner-box .loading-word {
  position: absolute;
  color: #ffffff;
  font-size: 0.95rem;
  transform: translateY(64px);
  text-align: center;
}
p.loading-title {
  font-size: 1.25rem;
  margin: 20px 10px 4px 10px;
}
#loading-box .spinner-box .configure-core {
  width: 100%;
  height: 100%;
  background-color: #37474f;
}
div.loaded div.loading-left-bg {
  transform: translate(-100%, 0);
}
div.loaded div.loading-right-bg {
  transform: translate(100%, 0);
}
div.loaded div.spinner-box {
  display: none !important;
}
.loader {
  position: absolute;
  top: calc(50% - 32px);
  left: calc(50% - 32px);
  width: 64px;
  height: 64px;
  border-radius: 50%;
  perspective: 800px;
  transition: all 0.7s cubic-bezier(0.42, 0, 0, 1.01);
}
.inner {
  position: absolute;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.inner.one {
  left: 0%;
  top: 0%;
  animation: rotate-one 1s linear infinite;
  border-bottom: 3px solid #efeffa;
}
.inner.two {
  right: 0%;
  top: 0%;
  animation: rotate-two 1s linear infinite;
  border-right: 3px solid #efeffa;
}
.inner.three {
  right: 0%;
  bottom: 0%;
  animation: rotate-three 1s linear infinite;
  border-top: 3px solid #efeffa;
}
@keyframes rotate-one {
  0% {
    transform: rotateX(35deg) rotateY(-45deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(35deg) rotateY(-45deg) rotateZ(360deg);
  }
}
@keyframes rotate-two {
  0% {
    transform: rotateX(50deg) rotateY(10deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(50deg) rotateY(10deg) rotateZ(360deg);
  }
}
@keyframes rotate-three {
  0% {
    transform: rotateX(35deg) rotateY(55deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(35deg) rotateY(55deg) rotateZ(360deg);
  }
}

‌js部分

//加载完成后执行
window.addEventListener(
  'load',
  function () {
    //载入动画
    $('#loading-box').attr('class', 'loaded')
    $('#bg').css(
      'cssText',
      'transform: scale(1);filter: blur(0px);transition: ease 1.5s;'
    )
    $('.cover').css('cssText', 'opacity: 1;transition: ease 1.5s;')
    $('#section').css(
      'cssText',
      'transform: scale(1) !important;opacity: 1 !important;filter: blur(0px) !important'
    )
  },
  false
)
正文完
 0
鹿泽
版权声明:本站原创文章,本文由 鹿泽 于 2024年8月13日 14:19:21 发表,共计 2717 字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。
如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
最新文章
WordPress SEO 优化之标签优化技巧

WordPress SEO 优化之标签优化技巧

今天我们来说一下 WordPress 文章标签优化的事情。在很多 WordPress SEO 优化教程中,都会...
WordPress SEO 之站外链接建设方法和技巧

WordPress SEO 之站外链接建设方法和技巧

站外链接(简称外链)是 WordPress SEO 过程中必不可少的一项工作,怎么做外链,从哪些方面着手,很多...
一次性删除WordPress文章所有评论的方法

一次性删除WordPress文章所有评论的方法

不少小伙伴在建设WordPress站点的时候都会开启文章评论等,作为网站良好的一个互动。但是这也给了很多垃圾评...
WordPress前台显示登录用户注册时间的方法

WordPress前台显示登录用户注册时间的方法

正常WordPress前台和后台都不会显示用户的注册时间,有时需要在前台显示登录用户的注册时间,可以用下面的代...
WordPress加个评论关闭时间提示的方法

WordPress加个评论关闭时间提示的方法

为了阻止垃圾评论,我们可以进入WP后台 → 设置 → 讨论,在讨论设置面勾选“自动关闭发布 14天后的文章上的...