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

uniapp自定义进度条(vue或原生开发修改html标签即可)

1.创建滚动条标签

<view class="progress-container"> <view class="progress-bar gradient" :style="{ width: progress + '%' }"></view> </view>

2.增加滚动条样式

/* 进度条容器 */ .progress-container { width: 100%; height: 20rpx; background-color: #f5f5f5; border-radius: 20rpx; overflow: hidden; .progress-bar { height: 100%; background: linear-gradient(90deg, #ff2d55 0%, #ff9500 25%, #ffcc00 50%, #4cd964 75%, #5ac8fa 100%); background-size: 200% 100%; animation: gradient-animation 3s ease infinite; } }

3.data创建字段progress 、animationId

progress :用来动态修改滚动条滚动距离

animationId : 防止重复启动

4.根据时间控制进度条速度

animateProgress() { if (this.animationId) return; //防止重复启动 const startTime = performance.now(); //获取时间源 let current = this.progress; let duration = 3000 //总时间 3秒 const step = (e) => { const elapsed = e - startTime; this.progress = (Math.min(elapsed / duration, 1)) * 100; if (this.progress < 100) { this.animationId = requestAnimationFrame(step); } else { console.log('进度完成!'); } }; this.animationId = requestAnimationFrame(step); }

5.完整代码

<template> <view class="progress-container"> <view class="progress-bar gradient" :style="{ width: progress + '%' }"></view> </view> </template> <script> export default { data() { return { progress : 0, animationId : null } }, onLoad(){ this.animateProgress() }, methods:{ animateProgress() { if (this.animationId) return; const startTime = performance.now(); let current = this.progress; let duration= 3000 const step = (e) => { const elapsed = e - startTime; this.progress = (Math.min(elapsed / duration, 1)) * 100; if (this.progress < 100) { this.animationId = requestAnimationFrame(step); } else { console.log('进度完成!'); } }; this.animationId = requestAnimationFrame(step); } } } </script> <style lang="scss" scoped> /* 进度条容器 */ .progress-container { width: 100%; height: 20rpx; background-color: #f5f5f5; border-radius: 20rpx; overflow: hidden; .progress-bar { height: 100%; background: linear-gradient(90deg, #ff2d55 0%, #ff9500 25%, #ffcc00 50%, #4cd964 75%, #5ac8fa 100%); background-size: 200% 100%; animation: gradient-animation 3s ease infinite; } } </style>

6.样式

7.扩展(暂停/继续)

isPaused:控制是否暂停 false为暂停 true为开始 初始化false

pauseStartTime:暂停时间

accumulatedPauseTime:暂停时间长度

开始
this.accumulatedPauseTime = performance.now() requestAnimationFrame(this.animateProgress)
暂停/继续
isPausedBtn(){ if (!this.animationId) return; if(!this.isPaused){ this.isPaused = true this.pauseStartTime = performance.now(); cancelAnimationFrame(this.animationId); }else{ this.isPaused = false this.accumulatedPauseTime += performance.now() - this.pauseStartTime; this.animationId = requestAnimationFrame(this.animateProgress); } }, animateProgress(e) { if (this.isPaused) return; let current = this.progress; let duration= 3000 const elapsed = e - this.accumulatedPauseTime; this.progress = (Math.min(elapsed / duration, 1)) * 100; if (this.progress < 100) { this.animationId = requestAnimationFrame(this.animateProgress); } else { console.log('进度完成!'); } this.animationId = requestAnimationFrame(this.animateProgress); }
http://www.jsqmd.com/news/717294/

相关文章:

  • 2025届毕业生推荐的十大AI写作网站实测分析
  • VS Code MCP协议集成实战(MCP v0.8.2深度适配手册)
  • Real Anime Z镜像安全机制:本地权重校验、SHA256签名验证与沙箱运行
  • 多维度拆透渲染引擎 第七篇【维度:生态】图形库、中间件与数据标准在渲染引擎中的角色
  • vue-beauty自定义组件开发教程:扩展你的组件库
  • 【OpenClaw最新版本】 命令行备忘录:高频操作与实战技巧
  • 2025_NIPS_Rethinking Memory and Communication Costs for Efficient Data Parallel Training of Large...
  • bge-large-zh-v1.5惊艳效果:中文学术摘要嵌入可视化与聚类图谱
  • 告别DQ线混战!手把手解析NAND SCA接口如何用CA通道提升SSD性能
  • 第4课:注意力机制入门【什么是“注意力”?】
  • NVIDIA NIM微服务:RTX AI PC上的生成式AI开发新范式
  • intv_ai_mk11惊艳案例:用intv_ai_mk11生成的5条工作效率建议被团队直接采用
  • 如何用Memtest86+彻底诊断电脑内存故障:新手完整指南
  • 告别电弧火花!用Arduino+过零检测模块实现交流电机软启动与调光
  • CST FAQ 008:CST-历史树
  • 【权威实测】Docker Compose vs. Dockerfile vs. Devcontainer.json:哪种远程容器初始化方式快47%?
  • 知从木牛瑞萨RH850 P1M-C软件算法优化实践CyberSecurity Application of ZC.MuNiu on Renesas RH850 ICUM
  • 【读书笔记】《臣服实验》
  • 开源免费的WPS AI 软件 察元AI文档助手:链路 012:structuredSystemPrompt 与单次 system 的关系
  • 全域数学三元本源公理体系 核心公式汇总表(永久典藏版)
  • Burp_Suite_Professional_2026.4
  • 终极指南:如何快速免费提取Ren‘Py游戏RPA归档文件
  • 基于AFSIM的空间目标动能拦截系统:最小化完整案例
  • 数据结构----插入排序
  • real-anime-z实战教程:用‘cherry blossom’+‘soft focus background’营造日系氛围感
  • OpCore Simplify:3步轻松搞定黑苹果OpenCore EFI配置的智能工具
  • 微服务-Docker
  • 2026MCX关键任务通信哪家好?融合通信厂商推荐与核心能力盘点 - 栗子测评
  • YOLOv13实战入门:快速上手图片和视频中的物体识别
  • GD32F470内存布局详解:为什么你的SRAM只有448KB,以及如何用RT-Thread的memheap管理那64KB TCMSRAM