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);
                }
相关推荐
宸翰19 小时前
uni-app 设置 Android 底部虚拟导航栏背景色
android·前端·uni-app
千逐6821 小时前
UniApp 鸿蒙实战:音视频与多媒体处理完全指南
华为·uni-app·harmonyos·鸿蒙
handsome09161 天前
windows开发uniapp如何上架app到app store
uni-app·上架
宠友信息1 天前
Redis 内容社区源码架构优化与即时通讯数据一致性处理
spring boot·后端·websocket·mysql·uni-app
CHB2 天前
uni-app x 蒸汽模式 iOS版 性能测试基准报告 Benchmark
ios·uni-app·swiftui
衍生星球2 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
编程猪猪侠3 天前
UniAppx安卓app实现左侧导航与右侧内容联动滚动
android·uni-app
千逐683 天前
Uniapp 鸿蒙实战之 AtomGit APP - 仓库详情与文件树浏览
服务器·microsoft·uni-app
万物得其道者成3 天前
uni-app App 热更新实现指南:从接口检查到下载安装重启(附带AI提示词)
uni-app
anyup3 天前
uView Pro 正式支持 MCP 协议!让 AI 秒懂你的组件库
前端·uni-app·ai编程