三十的博客

Vue3 项目开发技巧01:项目动画

发布时间
最后更新
阅读量 加载中...

弹跳方块加载动画

此演示仅在桌面端浏览器中可用

实现方式 1

主要依赖于定位、动画

需要在 main.js 中添加如下代码:

js
// 移除开屏加载动画
const removeLoading = () => {
  const element = document.getElementById("loading-box");
  element?.remove();
};

在 gin-vue-admin 项目中,是在 permission.js 中去调用 removeLoading 函数。

html
<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <link rel="icon" href="/favicon.ico" />
    <title></title>
    <style>
      body {
        margin: 0;
        --bg-color: #409eff;
      }
      #loading-box {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100vh;
        width: 100vw;
      }
      #loading-text {
        position: absolute;
        bottom: calc(50% - 100px);
        left: 0;
        width: 100%;
        text-align: center;
        color: #666;
        font-size: 14px;
      }
      #loading {
        position: absolute;
        top: calc(50% - 20px);
        left: calc(50% - 20px);
      }
      #box {
        width: 50px;
        height: 50px;
        background: var(--bg-color);
        animation: animate 0.5s linear infinite;
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 3px;
      }
      @keyframes animate {
        17% {
          border-bottom-right-radius: 3px;
        }
        25% {
          transform: translateY(9px) rotate(22.5deg);
        }
        50% {
          transform: translateY(18px) scale(1, 0.9) rotate(45deg);
          border-bottom-right-radius: 40px;
        }
        75% {
          transform: translateY(9px) rotate(67.5deg);
        }
        100% {
          transform: translateY(0) rotate(90deg);
        }
      }
      #shadow {
        width: 50px;
        height: 5px;
        background: #000;
        opacity: 0.1;
        position: absolute;
        top: 59px;
        left: 0;
        border-radius: 50%;
        animation: shadow 0.5s linear infinite;
      }
      .dark #shadow {
        background: #fff;
      }
      @keyframes shadow {
        50% {
          transform: scale(1.2, 1);
        }
      }
    </style>
  </head>

  <body>
    <div id="loading-box">
      <div id="loading">
        <div id="shadow"></div>
        <div id="box"></div>
      </div>
      <div id="loading-text">系统正在加载中...</div>
    </div>
    <div id="app"></div>
    <script type="module" src="./src/main.js"></script>
  </body>
</html>

实现方式 2

主要依赖于伪元素、动画。

优点:无需手动移除加载动画,当页面加载完成后,自动移除加载动画。

html
<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>wanglei-shuaige~</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        background-color: #f6fafd;
      }

      .loader-body {
        width: 100vw;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
      }
      .loader {
        width: 48px;
        height: 48px;
        margin: auto;
        position: relative;
      }

      .loader:before {
        content: "";
        width: 48px;
        height: 5px;
        background: #f0808050;
        position: absolute;
        top: 60px;
        left: 0;
        border-radius: 50%;
        animation: shadow324 0.5s linear infinite;
      }

      .loader:after {
        content: "";
        width: 100%;
        height: 100%;
        background: #f08080;
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 4px;
        animation: jump7456 0.5s linear infinite;
      }

      @keyframes jump7456 {
        15% {
          border-bottom-right-radius: 3px;
        }

        25% {
          transform: translateY(9px) rotate(22.5deg);
        }

        50% {
          transform: translateY(18px) scale(1, 0.9) rotate(45deg);
          border-bottom-right-radius: 40px;
        }

        75% {
          transform: translateY(9px) rotate(67.5deg);
        }

        100% {
          transform: translateY(0) rotate(90deg);
        }
      }

      @keyframes shadow324 {
        0%,
        100% {
          transform: scale(1, 1);
        }

        50% {
          transform: scale(1.2, 1);
        }
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="loader-body">
        <div class="loader"></div>
      </div>
    </div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>

全局过渡效果

同样的在 /index.html 中进行改造。

  1. 在根节点 html 上添加一个自定义类名。
html
<html lang="zh-cn" class="transition-colors"></html>
  1. <style> 中添加全局过渡效果。
css
.transition-colors {
  /* 
    指定了哪些CSS属性会有过渡效果
    - 文字颜色(color)
    - 背景色(background-color)
    - 边框颜色(border-color)
    - 文字装饰颜色(text-decoration-color)
    - SVG填充色(fill)
    - SVG描边色(stroke)
  */
  transition-property: color, background-color, border-color,
    text-decoration-color, fill, stroke;
  /* 定义了过渡的时间曲线 */
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  /* 定义了过渡的时间 */
  transition-duration: 150ms;
}

十字旋转加载动画

此演示仅在桌面端浏览器中可用

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>wanglei.live</title>
  </head>
  <style>
    .container {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .desc {
      font-size: 14px;
      color: #3273dc;
      margin-top: 10px;
    }

    .spin {
      width: 50px;
      /* 保持宽高比为1:1的正方形 */
      aspect-ratio: 1;
      /* farthest-side表示渐变会延伸到最远的边 */
      --_c: no-repeat radial-gradient(farthest-side, #3273dc 92%, #0000);
      background: var(--_c) top, /* 顶部渐变 */ var(--_c) left,
        /* 左侧渐变 */ var(--_c) right, /* 右侧渐变 */ var(--_c) bottom; /* 底部渐变 */
      background-size: 14px 14px; /* 每个渐变的尺寸 */
      animation: spin 1s infinite; /* 应用名为spin的动画 */
    }
    @keyframes spin {
      to {
        transform: rotate(0.5turn); /* 旋转180度(0.5圈) */
      }
    }
  </style>
  <body>
    <div class="container">
      <div class="spin"></div>
      <div class="desc">wanglei.live</div>
    </div>
  </body>
</html>

开发技巧

在进行 vue 开发时,可以考虑通过操作 dom 的方式将动画节点添加到#app节点下。而不是直接写在#app节点下。

这样的好处是用户可以自定义全局颜色(包含加载动画颜色)

旋转点阵加载动画

此演示仅在桌面端浏览器中可用

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vue.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>wanglei.live</title>
  </head>
  <body>
    <div id="app">
      <style>
        html,
        body,
        #app {
          width: 100%;
          height: 100%;
          padding: 0;
          margin: 0;
        }
        .loading-box {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
        }
        .loading-box .loading-wrap {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 98px;
        }
        .dot {
          position: relative;
          box-sizing: border-box;
          display: inline-block;
          width: 32px;
          height: 32px;
          font-size: 32px;
          transform: rotate(45deg);
          animation: ant-rotate 1.2s infinite linear;
        }
        .dot i {
          position: absolute;
          display: block;
          width: 14px;
          height: 14px;
          background-color: #409eff;
          border-radius: 100%;
          opacity: 0.3;
          transform: scale(0.75);
          transform-origin: 50% 50%;
          animation: ant-spin-move 1s infinite linear alternate;
        }
        .dot i:nth-child(1) {
          top: 0;
          left: 0;
        }
        .dot i:nth-child(2) {
          top: 0;
          right: 0;
          animation-delay: 0.4s;
        }
        .dot i:nth-child(3) {
          right: 0;
          bottom: 0;
          animation-delay: 0.8s;
        }
        .dot i:nth-child(4) {
          bottom: 0;
          left: 0;
          animation-delay: 1.2s;
        }

        @keyframes ant-rotate {
          to {
            transform: rotate(405deg);
          }
        }

        @keyframes ant-spin-move {
          to {
            opacity: 1;
          }
        }
      </style>
      <div class="loading-box">
        <div class="loading-wrap">
          <span class="dot dot-spin"><i></i><i></i><i></i><i></i></span>
        </div>
      </div>
    </div>
    <script>
      const globalState = JSON.parse(
        window.localStorage.getItem("geeker-global")
      );
      if (globalState) {
        const dot = document.querySelectorAll(".dot i");
        const html = document.querySelector("html");
        dot.forEach((item) => (item.style.background = globalState.primary));
        if (globalState.isDark) html.style.background = "#141414";
      }
    </script>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>
#项目开发技巧 #Vue3