vue项目首屏加载动画

首先在index.html中做动画,由于拿不到dom元素。需要手动去创建

let animateTimer;
    // 创建loading的盒子
    function loadingBox() {
      const html = document.documentElement
      const loadBox = document.createElement("div");
      loadBox.className = "first-loading"
      html.appendChild(loadBox);
    }

    // 创建介绍的盒子
    function introBox() {
      const loadBox = document.querySelector(".first-loading")
      const introBox = document.createElement("div");
      introBox.className = "load-introduce"
      introBox.innerText = "qq";
      loadBox.appendChild(introBox);
    }

    // 创建动画盒子
    function animateBox() {
      const loadBox = document.querySelector(".first-loading")
      const loadAnimate = document.createElement("div");
      loadAnimate.className = "load-animate";
      loadAnimate.innerText = "loading";
      const ul = document.createElement("ul");
      ul.className = "load-ul";
      [1, 2, 3].forEach(item => {
        const li = document.createElement("li");
        li.className = "load-li";
        ul.appendChild(li);
      })
      loadAnimate.appendChild(ul);
      loadBox.appendChild(loadAnimate);
    }

    // 改变introduce的内容
    function changeIntro() {
      const list = [
        "qq",
        "ww",
        "ee"
      ]
      let index = 0;
      animateTimer = setInterval(() => {
        console.log(12);
        if (index === 2) {
          index = 0;
        } else {
          index++;
        }
        const intro = document.querySelector(".load-introduce");
        if (intro) intro.innerText = list[index];
      }, 2000);
    }

    loadingBox();
    introBox();
    animateBox();
    changeIntro();

样式

 .first-loading {
      width: 100vw;
      height: 100vh;
      background: no-repeat url("");
      overflow: hidden;
      color: #FFF;
    }

    .load-introduce {
      margin: 200px auto;
      text-align: center;
      font-size: 50px;
      padding: 0 70px;
    }

    .load-animate {
      position: fixed;
      top: 65%;
      left: 50%;
      transform: translateX(-50%);
      text-align: center;
      font-style: italic;
      letter-spacing: 5px;
      display: flex;
      font-size: 25px;
      justify-content: center;
    }

    .load-ul {
      margin-top: 15px;
      list-style: none;
      display: flex;
      justify-content: center;
    }

    .load-li {
      margin-left: 10px;
      height: 15px;
      width: 15px;
      background-color: #FFF;
      border-radius: 100%;
      display: inline-block;
      -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
      animation: bouncedelay 1.4s infinite ease-in-out;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
    }

    .load-li:nth-of-type(1) {
      -webkit-animation-delay: -0.32s;
      animation-delay: -0.32s;
    }

    .load-li:nth-of-type(2) {
      -webkit-animation-delay: -0.16s;
      animation-delay: -0.16s;
    }

    .load-introduce::after {}

    @-webkit-keyframes introduce-show {
      0% {}
    }

    @-webkit-keyframes bouncedelay {

      0%,
      80%,
      100% {
        -webkit-transform: scale(0.0)
      }

      40% {
        -webkit-transform: scale(1.0)
      }
    }

    @keyframes bouncedelay {

      0%,
      80%,
      100% {
        transform: scale(0.0);
        -webkit-transform: scale(0.0);
      }

      40% {
        transform: scale(1.0);
        -webkit-transform: scale(1.0);
      }
    }

动画需要在App.vue/App.tsx中确定删除dom以及移除定时器

    // 删除动画,定时器
    onMounted(() => {
      // @ts-ignore
      if (animateTimer) clearInterval(animateTimer);
      if (document.querySelector(".first-loading"))
        document.querySelector(".first-loading")!.remove();
    });

妥!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中实现首屏加载动画,你可以参考以下步骤: 1. 首先,在你的Vue项目中创建一个全局加载组件,用于展示加载动画。你可以在组件中使用CSS或动画库来定义你想要的加载效果。 2. 在你的根组件(通常是App.vue)中引入并注册这个全局加载组件。 3. 在页面加载的过程中,使用Vue的生命周期钩子函数(如created或mounted)来控制加载动画的显示和隐藏。在页面加载之前,显示加载动画加载完成后,隐藏加载动画。 4. 如果你想要自定义加载动画的样式,你可以在全局加载组件中定义样式,并在组件中使用<slot>标签来插入内容。 5. 最后,你可以根据你的需求在加载动画组件中添加文本或图标来显示加载状态,以提供更好的用户体验。 请注意,加载动画和数据请求的组件不应同时使用,以免出现两个加载效果。另外,要谨慎使用全局加载动画,因为它会在整个应用程序中显示。 这是一个简单的示例代码,展示了如何在Vue3中实现首屏加载动画: ```vue // 在全局加载组件中定义加载动画的样式和内容 <template> <div class="loading"> <div class="loader"></div> <p>Loading...</p> </div> </template> <script> export default { name: 'Loading', // 根据需要可以添加进入、离开过渡效果 // 可以使用CSS或动画库定义加载动画的样式 // 在created或mounted钩子函数中控制显示和隐藏 created() { // 显示加载动画 }, mounted() { // 隐藏加载动画 } } </script> // 在根组件中引入并注册全局加载组件 <template> <div id="app"> <router-view /> <Loading v-if="isLoading" /> </div> </template> <script> import Loading from '@/components/Loading.vue'; export default { name: 'App', components: { Loading }, data() { return { isLoading: true } }, created() { // 页面加载前显示加载动画 }, mounted() { // 页面加载完成后隐藏加载动画 } } </script> ``` 这样,当你的Vue应用程序加载时,首屏将会显示加载动画加载完成后加载动画将会被隐藏。你可以根据自己的需求自定义加载动画的样式和内容。记得根据实际情况在合适的生命周期钩子函数中控制加载动画的显示和隐藏。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue实现页面加载动画效果](https://download.csdn.net/download/weixin_38721565/12772582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue实现网页首屏加载动画、页面内请求数据加载loading](https://blog.csdn.net/weixin_48337566/article/details/127896990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值