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

1.创建滚动条标签

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

2.增加滚动条样式

css 复制代码
/* 进度条容器 */
.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.根据时间控制进度条速度

javascript 复制代码
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.完整代码

html 复制代码
<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:暂停时间长度

开始
javascript 复制代码
this.accumulatedPauseTime = performance.now()
requestAnimationFrame(this.animateProgress)
暂停/继续
javascript 复制代码
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);
                }
相关推荐
用户6990304848758 小时前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_9 小时前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Geek_Vison16 小时前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
CHB1 天前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos
2501_915918412 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
斯内普吖2 天前
(开源)高校素拓分管理系统小程序实战指南 基于 Java + SpringBoot + uni-app + Vue + MySQL
java·spring boot·mysql·小程序·uni-app·开源
海阔天空66882 天前
uniapp开启调试模式
uni-app·uniapp开启调试模式
anyup2 天前
分享 5 套 uni-app 实用主题,一键适配暗黑模式
前端·uni-app·视觉设计
gg159357284603 天前
Uni-app跨平台开发全解课程:从零基础到企业级多端落地实战
vue.js·uni-app
xshirleyl4 天前
uniapp小兔鲜儿day3
uni-app