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

21 篇文章 2 订阅

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>

最后就是生成背景随机数

  1. 创建一个数组,路径要以这种格式写(当然你也可以用更好的方法来拆开写)
 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",
    },
  ]);
  1. 获取数组对象中的随机数
let imgName  = arr[Math.floor(Math.random() * arr.length)].bg;
let style = "background: url('"+ imgName +" ')"
  1. 放在计算属性中
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;
});
  1. 最后将计算属性通过动态style放在标签中 <div class="com__box" :style="backgourndStyle"></div>

看下效果:
请添加图片描述
中间闪烁的其他页面忽略即可,本章是只讲述了自己封装的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>


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值