当前位置: 首页 > news >正文

微信小程序中实现进入页面时数字跳动效果(自定义animate-numbers组件)

微信小程序中实现进入页面时数字跳动效果

  • 1. 组件定义,新建```animate-numbers```组件
    • 1.1 index.js
    • 1.2 wxml
    • 1.3 wxss
  • 2. 使用组件

1. 组件定义,新建animate-numbers组件

1.1 index.js

// components/animate-numbers/index.js
Component({properties: {number: {type: Number,value: 0},duration: {type: Number,value: 1000}},data: {displayNumber: 0,animationFrameId: null},observers: {'number': function (newNumber) {this.animateNumber(newNumber);}},methods: {animateNumber(targetNumber) {const start = this.data.displayNumber;//旧值const end = targetNumber;//新值const duration = this.properties.duration;const increment = (end - start) / (duration / 16); // 假设每秒60帧,每帧间隔约16mslet current = start;if(this.data.animationFrameId){clearInterval(this.data.animationFrameId);}const animate = () => {current += increment;if ((increment > 0 && current >= end) || (increment < 0 && current <= end)) {clearInterval(this.data.animationFrameId);this.setData({ displayNumber: end });} else {this.setData({ displayNumber: Math.round(current) });}};this.data.animationFrameId = setInterval(animate, 16);}},// 组件被移除时清除定时器detached() {clearInterval(this.data.animationFrameId);}
});

1.2 wxml

<view>{{displayNumber}}</view>

1.3 wxss

page {font-size: 48rpx;font-weight: bold;
}

2. 使用组件

"animate-numbers": "../../../components/animate-numbers/index"

 <animate-numbers number="{{attendanceInfo.month_avg_days}}" duration="1000"/>
http://www.lryc.cn/news/528158.html

相关文章:

  • 【huawei】云计算的备份和容灾
  • Vue.js组件开发-实现下载时暂停恢复下载
  • TCP是怎么判断丢包的?
  • python爬虫入门(一) - requests库与re库,一个简单的爬虫程序
  • 2025年数学建模美赛 A题分析(3)楼梯使用方向偏好模型
  • 复古壁纸中棕色系和米色系哪个更受欢迎?
  • 编译安装PaddleClas@openKylin(失败,安装好后报错缺scikit-learn)
  • t113_can增加驱动
  • 达梦数据库建用户,键库脚本
  • 上海亚商投顾:沪指冲高回落 大金融板块全天强势 上海亚商投
  • MySQL 的索引类型【图文并茂】
  • 天聚地合:引领API数据流通服务,助力数字经济发展
  • 【反悔堆】【hard】力扣871. 最低加油次数
  • electron typescript运行并设置eslint检测
  • 服务器上安装Nginx详细步骤
  • Timeout or no response waiting for NATS JetStream server
  • 5.2 软件需求分析
  • DF 开发1
  • 【现代深度学习技术】深度学习计算 | 参数管理
  • 团体程序设计天梯赛-练习集——L1-024 后天
  • JVM栈溢出线上环境排查
  • Java实现FIFO缓存策略实战
  • set集合
  • 【数据结构】 并查集 + 路径压缩与按秩合并 python
  • 无耳科技 Solon v3.0.7 发布(2025农历新年版)
  • UART、I2C和SPI对比
  • Vue 响应式渲染 - 待办事项简单实现
  • ResNeSt: Split-Attention Networks论文学习笔记
  • 澳洲硕士毕业论文写作中如何把握主题
  • STM32 LED呼吸灯