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);
                }
相关推荐
西洼工作室4 小时前
unipp+vue3+python h5+app极验验证码集成全流程解析
前端·uni-app·全栈·极验
RuoyiOffice13 小时前
SpringBoot+Vue3 实现 OA 公文外来文与归档台账:外部收文、BPM办理、三类公文统一归档
spring boot·微服务·uni-app·vue·ruoyi·anti-design-vue·ruoyioffice
云起SAAS1 天前
私域直播系统UniApp源码 多商户商城+直播带货 微信小程序+H5+安卓iOS
android·微信小程序·uni-app·私域直播系统
专科3年的修炼3 天前
uni-app移动应用开发第四章
开发语言·javascript·uni-app
q5507071773 天前
uniapp/uniappx实现原生图片编辑涂鸦、贴图、滤镜、旋转、裁剪等
uni-app
计算机学姐4 天前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app
2501_915921434 天前
HTTPS前端劫持 新一代流量劫持解决方案
前端·网络协议·ios·小程序·https·uni-app·iphone
爱怪笑的小杰杰4 天前
优化 UniApp 日历组件的多语言切换:告别 setLocale 引起的 App 重启
java·前端·uni-app
计算机学姐5 天前
基于微信小程序的宠物服务系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·微信小程序·uni-app·宠物
2501_915909065 天前
iOS应用签名的三种方法全解析:从官方到第三方工具
android·ios·小程序·https·uni-app·iphone·webview