Vue3项目使用 wow.js 让页面滚动更有趣~

简介: 本文介绍了如何在Vue3项目中集成wow.js库,通过实现滚动动画效果来增强页面的动态性和趣味性,并提供了详细的使用示例和参数说明。

前言

项目是基于Vue3+Element plus框架设计的,本来使用Element plus的Loading加载动效已经是满足项目需求的,但是感觉AntDesign的加载动效图标好看一点点,于是自行实现一个基于Vue3,以及仿AntDesign的加载动效的示例。其主要用到Vue3的createVNode和render函数。

一、示例代码

(1)/src/utils/antdLodingUtil/antdLoding.vue

<template>
  <transition name="animation">
    <div class="diy-loading-wrap" v-if="isOpen">
     <div class="diy-loading-wrap-content">

      <div class="diy-loading-wrap-content-spin">
        <div class="ant-spin ant-spin-spinning">
          <span class="ant-spin-dot ant-spin-dot-spin">
            <i class="ant-spin-dot-item"></i>
            <i class="ant-spin-dot-item"></i>
            <i class="ant-spin-dot-item"></i>
            <i class="ant-spin-dot-item"></i>
          </span>
        </div>
      </div>

      <p class="diy-loading-wrap-content-p">{
   
   {
   
    text }}</p>
     </div>
    </div>
  </transition>
</template>

<script>
export default {
   
   
  data() {
   
   
    return {
   
   
      isOpen: false,
      text: '数据正在加载中...',
    }
  },
}
</script>

<style lang="less" scoped>
  :root {
   
   
    --el-mask-color: rgba(255, 255, 255, 0.9);
    --el-transition-duration: 0.3s;
    --el-transition-duration-fast: 0.2s;
    --el-loading-spinner-size: 42px;
    --el-loading-fullscreen-spinner-size: 50px;
  }

  /* animation 动画 */
  .animation-enter-active{
   
   
    animation: animationAction 0.3s;
  }

  .animation-leave-active{
   
   
    animation: animationAction 0.5s reverse;
  }

  @keyframes animationAction {
   
   
    0% {
   
   
      opacity: 0;
    }

    100% {
   
   
      opacity: 1;
    }
  }
  /* / animation 动画 */

  .diy-loading-wrap {
   
   
    position: absolute;
    z-index: 2000;
    background-color: var(--el-mask-color);
    margin: 0;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    transition: opacity var(--el-transition-duration);
  }

  .diy-loading-wrap .diy-loading-wrap-content {
   
   
    top: 50%;
    margin-top: calc((0px - var(--el-loading-spinner-size)) / 2);
    width: 100%;
    text-align: center;
    position: absolute;
  }

  .diy-loading-wrap .diy-loading-wrap-content .diy-loading-wrap-content-spin {
   
   

    .ant-spin {
   
   
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      color: #000000d9;
      font-size: 14px;
      font-variant: tabular-nums;
      line-height: 1.5715;
      list-style: none;
      font-feature-settings: tnum;
      position: absolute;
      display: none;
      color: #1890ff;
      text-align: center;
      vertical-align: middle;
      opacity: 0;
      transition: transform .3s cubic-bezier(.78,.14,.15,.86)
    }

    .ant-spin-spinning {
   
   
      position: static;
      display: inline-block;
      opacity: 1
    }

    .ant-spin-dot {
   
   
      position: relative;
      display: inline-block;
      font-size: 20px;
      width: 1em;
      height: 1em
    }

    .ant-spin-dot-spin {
   
   
      transform: rotate(45deg);
      animation: antRotate 1.2s infinite linear
    }

    .ant-spin-dot-item {
   
   
      position: absolute;
      display: block;
      width: 9px;
      height: 9px;
      background-color: #1890ff;
      border-radius: 100%;
      transform: scale(.75);
      transform-origin: 50% 50%;
      opacity: .3;
      animation: antSpinMove 1s infinite linear alternate
    }

    .ant-spin-dot-item:nth-child(1) {
   
   
      top: 0;
      left: 0;
    }
    .ant-spin-dot-item:nth-child(2) {
   
   
      top: 0;
      right: 0;
      animation-delay: .4s;
    }
    .ant-spin-dot-item:nth-child(3) {
   
   
      right: 0;
      bottom: 0;
      animation-delay: .8s;
    }

    .ant-spin-dot-item:nth-child(4) {
   
   
      bottom: 0;
      left: 0;
      animation-delay: 1.2s;
    }

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

    @keyframes antSpinMove {
   
   
      to {
   
   
        opacity: 1;
      }
    }
  }

  .diy-loading-wrap .diy-loading-wrap-content .diy-loading-wrap-content-p {
   
   
    margin: 5px auto 0 auto;
    color: #1890ff;
    font-size: 14px;
  }  
</style>

(2)/src/utils/antdLodingUtil/antdLoding.js

import {
   
    createVNode, render } from 'vue'
import loading from './antdLoding.vue'

let instance

/**
 * 打开仿 Antd 加载的方法
 * @param {*} option 
 */
function showLoading(option) {
   
   
  instance = createVNode(loading, option)
  render(instance, document.querySelector('body'))
  instance.component.data.isOpen = true
}

/**
 * 关闭仿 Antd 加载的方法
 */
function hideLoading() {
   
   
  if (instance) {
   
   
    instance.component.data.isOpen = false
  }
}

export {
   
   
  showLoading,
  hideLoading
}

(3)/src/views/Example/AntdLoding/index.vue

<template>
    <div style="position: fixed; z-index: 9999; padding: 100px">
      <el-button @click="handleOpenAntdLoding($event)" type="primary" size="small">open</el-button>
      <el-button @click="handleCloseAntdLoding($event)" type="danger" size="small">close</el-button>
    </div>
</template>

<script>
export default {
   
   
  data() {
   
   
    return {
   
   

    }
  },
  methods: {
   
   
    /**
     * 打开仿 Antd 加载的句柄方法
     */
    handleOpenAntdLoding(evt) {
   
   
      this.$elementUtil.handleElButtonBlur(evt)
      this.$antdLodingUtil.showLoading({
   
    author: '帅龍之龍' })
    },

    /**
     * 关闭仿 Antd 加载的句柄方法
     */
    handleCloseAntdLoding(evt) {
   
   
      this.$elementUtil.handleElButtonBlur(evt)
      this.$antdLodingUtil.hideLoading()
    }
  }
}
</script>

(4)/src/main.ts

// 引入仿Antd加载器工具并配置为全局方法
import * as antdLodingUtil from '@/utils/antdLodingUtil/antdLoding'
app.config.globalProperties.$antdLodingUtil = antdLodingUtil

二、运行效果

目录
相关文章
|
1月前
|
数据采集 资源调度 JavaScript
Node.js 适合做什么项目?
【8月更文挑战第4天】Node.js 适合做什么项目?
85 5
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的销售项目流程化管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的销售项目流程化管理系统附带文章源码部署视频讲解等
54 3
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的项目申报管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的项目申报管理系统附带文章源码部署视频讲解等
45 3
|
14天前
|
JSON 前端开发 JavaScript
|
18天前
|
JavaScript
Vue项目打包后都产生了哪些JS请求?
【8月更文挑战第19天】Vue项目打包后都产生了哪些JS请求?
45 0
Vue项目打包后都产生了哪些JS请求?
|
1月前
|
JavaScript Linux 开发工具
开源项目:使用 Atom-Electron 和 Vue.js 制作的简单 RSS 阅读器!!
开源项目:使用 Atom-Electron 和 Vue.js 制作的简单 RSS 阅读器!!
|
1月前
|
JavaScript
Vue进阶篇(二)-彻底改变 Vue.js 状态管理:深入探讨Vue 3 Setup中的DefineModel
Vue进阶篇(二)-彻底改变 Vue.js 状态管理:深入探讨Vue 3 Setup中的DefineModel
|
12天前
|
JavaScript 前端开发 API
解锁前端开发新境界:Vue.js携手Webpack,打造高效构建流程,你的项目值得拥有!
【8月更文挑战第30天】随着前端技术的发展,模块化与组件化趋势愈发显著。Vue.js 以其简洁的 API 和灵活的组件系统,深受开发者喜爱;Webpack 则凭借强大的模块打包能力成为前端工程化的基石。两者结合,不仅简化了组件编写与引用,还通过模块热替换、代码分割等功能大幅提升开发效率。本文将通过具体示例,展示如何利用 Vue.js 和 Webpack 构建高效、有序的前端开发环境。从安装配置到实际应用,逐步解析这一组合的优势所在。
32 0
|
1月前
|
数据采集 资源调度 JavaScript
Node.js 适合做高并发、I/O密集型项目、轻量级实时应用、前端构建工具、命令行工具以及网络爬虫和数据处理等项目
【8月更文挑战第4天】Node.js 适合做高并发、I/O密集型项目、轻量级实时应用、前端构建工具、命令行工具以及网络爬虫和数据处理等项目
36 5
|
1月前
|
设计模式 JavaScript 前端开发
小白请看 JS大项目宝典:设计模式 教你如何追到心仪的女神
小白请看 JS大项目宝典:设计模式 教你如何追到心仪的女神