当前位置:   article > 正文

uniapp实现红包动画效果(vue3)_vue nui-app 红包功能

vue nui-app 红包功能

效果演示

首先安装CSS动画库animate.css依赖

yarn add animate.css

打开main.ts文件引入

import 'animate.css'

这两张图片放入static文件夹下

用到的图片red1.png

用到的图片red2.png

红包整体主要分三部分 红包头部 中部 底部

  1. <template>
  2. <view>
  3. <button @tap="red = true">显示红包</button>
  4. </view>
  5. <!-- 红包特效遮罩层 -->
  6. <view v-show="red" class="cover">
  7. <!-- 红包整体 -->
  8. <view class="redBig animate__animated animate__bounceIn" :key="redIndex">
  9. <!-- 头部 -->
  10. <view :animation="redHead" class="redBigHead">
  11. <image @tap="redHeadAnimation" class="" src="/static/red1.png" mode=""></image>
  12. </view>
  13. <!-- 中间 -->
  14. <view :animation="redMid" class="redBigMid">
  15. <view class="text1">
  16. 恭喜您获得了
  17. </view>
  18. <view class="text2">
  19. 100.00
  20. </view>
  21. <view class="text3">
  22. 红包余额可去钱包查询
  23. </view>
  24. </view>
  25. <!-- 底部 -->
  26. <view class="redBigBottom">
  27. <!-- 打开红包后展示 -->
  28. <view @tap="redBagAnimationRenew" v-show="redBottom" class="button animate__animated animate__bounceIn">
  29. 开心收下
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>

使用uni.createAnimation()创建动画实例并进行相关操作具体查看官方文档

  1. <script setup lang="ts">
  2. import {
  3. ref,
  4. getCurrentInstance
  5. } from 'vue'
  6. import {
  7. onShow
  8. } from '@dcloudio/uni-app'
  9. const {
  10. proxy
  11. } = getCurrentInstance() as any
  12. //红包动画
  13. let red = ref(false) //红包显示
  14. let redIndex = ref(0) //红包组件刷新
  15. onShow(() => {
  16. let animation = uni.createAnimation()
  17. proxy.animation = animation
  18. })
  19. //头部动画
  20. let redHead = ref({})
  21. function redHeadAnimation() {
  22. proxy.animation.translateY(-300).opacity(0).step({
  23. duration: 500,
  24. timingFunction: 'ease-in-out',
  25. })
  26. redHead.value = proxy.animation.export()
  27. redBottom.value = true
  28. setTimeout(() => {
  29. redMidAnimation()
  30. }, 300)
  31. }
  32. //中部动画
  33. let redMid = ref({})
  34. function redMidAnimation() {
  35. proxy.animation.translateY(-120).opacity(1).step({
  36. duration: 500,
  37. timingFunction: 'ease-in-out',
  38. })
  39. redMid.value = proxy.animation.export()
  40. }
  41. //红包底部按钮显示
  42. let redBottom = ref(false)
  43. //重置红包
  44. function redBagAnimationRenew() {
  45. red.value = false
  46. redBottom.value = false
  47. redHead.value = {}
  48. redMid.value = {}
  49. redIndex.value++
  50. }
  51. </script>

除了红包底部 头部和中部需要定位

  1. <style lang="scss">
  2. .cover {
  3. position: fixed;
  4. left: 0;
  5. top: 0;
  6. bottom: 0;
  7. right: 0;
  8. background: rgba(000, 000, 000, 0.5);
  9. display: flex;
  10. align-items: center;
  11. justify-content: center;
  12. }
  13. .redBig {
  14. position: relative;
  15. display: flex;
  16. flex-direction: column;
  17. align-items: center;
  18. }
  19. .redBigHead {
  20. position: absolute;
  21. z-index: 2;
  22. image {
  23. width: 550rpx;
  24. height: 440rpx;
  25. }
  26. }
  27. .redBigMid {
  28. width: 508rpx;
  29. height: 350rpx;
  30. background-color: #fff;
  31. border-radius: 24rpx;
  32. display: flex;
  33. flex-direction: column;
  34. align-items: center;
  35. color: #FF4545;
  36. position: absolute;
  37. z-index: 0;
  38. margin-top: 260rpx;
  39. opacity: 0;
  40. .text1 {
  41. margin-top: 20rpx;
  42. font-size: 32rpx;
  43. }
  44. .text2 {
  45. margin-top: 30rpx;
  46. font-size: 70rpx;
  47. }
  48. .text3 {
  49. margin-top: 30rpx;
  50. }
  51. }
  52. .redBigBottom {
  53. width: 550rpx;
  54. height: 331rpx;
  55. background-image: url('../../static/red2.png');
  56. background-size: 100% 100%;
  57. margin-top: 280rpx;
  58. z-index: 1;
  59. .button {
  60. background: linear-gradient(to bottom, #FEE3AD 50%, #FEB05C);
  61. color: #BC0D0D;
  62. margin-left: 84rpx;
  63. margin-right: 84rpx;
  64. padding-top: 30rpx;
  65. padding-bottom: 30rpx;
  66. border-radius: 100rpx;
  67. text-align: center;
  68. margin-top: 150rpx;
  69. }
  70. }
  71. </style>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读