Vue3+ts——动画Loading以及引入本地随机背景效果

简介: 动画Loading以及引入本地随机背景效果

Vue3+ts——随机生成背景图片

我这里是采用loading为例子制作的随机背景效果(底部附上代码和成品效果展示)

这里需要注意一下,数组的格式必须要用下面这种格式创建对象

new URL("../assets/image/myFileImg/nv.png", import.meta.url).href;

或者使用这种方法引入本地图片路径

<div :style="'background-image: url(' + state.icon+ '); background-size:100% 100%;width:100%;height:100vh'">
</div>
const state = reactive({
  icon: new URL("../assets/image/index1.jpg", import.meta.url).href,
});

如果使用直接使用“…/assets/image/myFileImg/nv.png” Vue3是不支持的。

随机色最好是使用computed去写,这里原因就不多说了。

首先先做出加载的动画效果

<template>
  <div class="com__box" :style="backgourndStyle">
    <!-- loading -->
    <div class="loading">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
      <div class="shape shape-4"></div>
    </div>
  </div>
</template>
<style lang="less" scoped>
.com__box {
  width: 100%;
  // background-color: transparent;
  background-size: cover !important;
  background-repeat: no-repeat !important;
  background-position: center center !important;
  display: flex;
  height: calc(100vh - 50px);
  align-items: center;
  justify-content: center;
}
.loading {
  width: 20px;
  height: 20px;
  position: relative;
  animation: animationContainer 1s ease infinite;
}
.shape {
  width: 10px;
  height: 10px;
  position: absolute;
}
.shape-1 {
  background-color: #845ec2;
  left: 0;
  border-top-left-radius: 100%;
  animation: animationShape1 0.5s ease infinite alternate;
}
.shape-2 {
  background-color: #009efa;
  right: 0;
  border-top-right-radius: 100%;
  animation: animationShape2 0.5s ease infinite alternate;
}
.shape-3 {
  background-color: #00d2fc;
  bottom: 0;
  border-bottom-left-radius: 100%;
  animation: animationShape3 0.5s ease infinite alternate;
}
.shape-4 {
  background-color: #4ffbdf;
  right: 0;
  bottom: 0;
  border-bottom-right-radius: 100%;
  animation: animationShape4 0.5s ease infinite alternate;
}
@keyframes animationContainer {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes animationShape1 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-3px, -3px);
  }
}
@keyframes animationShape2 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(3px, -3px);
  }
}
@keyframes animationShape3 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-3px, 3px);
  }
}
@keyframes animationShape4 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(3px, 3px);
  }
}
</style>

效果做好后可以做一个文字的动画效果。

文字动画效果

cssAnimatopy

下方代码中的 @import "../assets/css/animatopy.css";在上方的链接中查看即可

<template>
  <div class="com__box" :style="backgourndStyle">
    <!-- loading -->
    <div class="loading">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
      <div class="shape shape-4"></div>
    </div>
    <div class="p animated rubberBand">
      加载中请稍后...
    </div>
  </div>
</template>
<style lang="less" scoped>
/* 这里需要引入创建动画样式 */
@import "../assets/css/animatopy.css";
.com__box {
  width: 100%;
  background-size: cover !important;
  background-repeat: no-repeat !important;
  background-position: center center !important;
  display: flex;
  height: calc(100vh - 50px);
  align-items: center;
  justify-content: center;
  .p {
    margin-left: 20px;
    font-size: 24px;
    font-family: FBDXYT;
    font-weight: bold;
    background-image: -webkit-linear-gradient(
      left,
      #845ec2,
      #009efa,
      #00d2fc,
      #4ffbdf
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}
</style>

最后就是生成背景随机数

创建一个数组,路径要以这种格式写(当然你也可以用更好的方法来拆开写)

let arr = reactive([
    { bg: new URL("../assets/image/01.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/02.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/03.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/04.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/05.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/06.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/07.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/08.jpg",import.meta.url).href },
    {
      bg: "../assets/image/01.jpg",
    },
  ]);

获取数组对象中的随机数

let imgName  = arr[Math.floor(Math.random() * arr.length)].bg;
let style = "background: url('"+ imgName +" ')"

放在计算属性中

const backgourndStyle = computed(() => {
  let arr = reactive([
    { bg: new URL("../assets/image/01.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/02.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/03.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/04.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/05.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/06.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/07.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/08.jpg",import.meta.url).href },
    {
      bg: "../assets/image/01.jpg",
    },
  ]);
  let imgName  = arr[Math.floor(Math.random() * arr.length)].bg;
  let style = "background: url('"+ imgName +" ')"
  return style;
});

最后将计算属性通过动态style放在标签中

<div class="com__box" :style="backgourndStyle"></div>

看下效果:

3b2a9f4266114dd49cb305b1b8833ef2.gif

中间闪烁的其他页面忽略即可,本章是只讲述了自己封装的Loading组件的动画效果,大家直接使用完整代码即可

完整代码:

<template>
  <div class="com__box" :style="backgourndStyle">
    <!-- loading -->
    <div class="loading">
      <div class="shape shape-1"></div>
      <div class="shape shape-2"></div>
      <div class="shape shape-3"></div>
      <div class="shape shape-4"></div>
    </div>
    <div class="p animated rubberBand">
      加载中请稍后...
    </div>
  </div>
</template>
<script lang='ts' setup>
import { ref, onMounted, reactive, computed } from "vue";
const backgourndStyle = computed(() => {
  let arr = reactive([
    { bg: new URL("../assets/image/01.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/02.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/03.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/04.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/05.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/06.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/07.jpg",import.meta.url).href },
    { bg: new URL("../assets/image/08.jpg",import.meta.url).href },
    {
      bg: "../assets/image/01.jpg",
    },
  ]);
  let imgName  = arr[Math.floor(Math.random() * arr.length)].bg;
  let style = "background: url('"+ imgName +" ')"
  return style;
});
</script>
<style lang="less" scoped>
@import "../assets/css/animatopy.css";
.com__box {
  width: 100%;
  // background-color: transparent;
  background-size: cover !important;
  background-repeat: no-repeat !important;
  background-position: center center !important;
  display: flex;
  height: calc(100vh - 50px);
  align-items: center;
  justify-content: center;
  .p {
    margin-left: 20px;
    font-size: 24px;
    font-family: FBDXYT;
    font-weight: bold;
    background-image: -webkit-linear-gradient(
      left,
      #845ec2,
      #009efa,
      #00d2fc,
      #4ffbdf
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}
.loading {
  width: 20px;
  height: 20px;
  position: relative;
  animation: animationContainer 1s ease infinite;
}
.shape {
  width: 10px;
  height: 10px;
  position: absolute;
}
.shape-1 {
  background-color: #845ec2;
  left: 0;
  border-top-left-radius: 100%;
  animation: animationShape1 0.5s ease infinite alternate;
}
.shape-2 {
  background-color: #009efa;
  right: 0;
  border-top-right-radius: 100%;
  animation: animationShape2 0.5s ease infinite alternate;
}
.shape-3 {
  background-color: #00d2fc;
  bottom: 0;
  border-bottom-left-radius: 100%;
  animation: animationShape3 0.5s ease infinite alternate;
}
.shape-4 {
  background-color: #4ffbdf;
  right: 0;
  bottom: 0;
  border-bottom-right-radius: 100%;
  animation: animationShape4 0.5s ease infinite alternate;
}
@keyframes animationContainer {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes animationShape1 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-3px, -3px);
  }
}
@keyframes animationShape2 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(3px, -3px);
  }
}
@keyframes animationShape3 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-3px, 3px);
  }
}
@keyframes animationShape4 {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(3px, 3px);
  }
}
</style>
相关文章
|
6月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
497 1
|
6月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
907 139
|
6月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
563 0
|
7月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
797 11
|
8月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
852 1
|
8月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
454 0
|
9月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
207 0
|
6月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
538 137
|
7月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
629 2
|
9月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
451 0

热门文章

最新文章

下一篇
开通oss服务