如何让jQuery Circle Progress更新数据时不从0开始动画?
实现jquery-circle-progress从当前值平滑过渡到目标值
问题说明
使用jquery-circle-progress插件时,点击更新按钮后进度条会从0开始重新执行动画,需要改为从当前进度平滑过渡到目标值。
解决方案
核心是利用插件的animationStartValue参数,在更新时将该参数设置为进度条的当前值,同时指定目标value,插件就会自动从当前值过渡到目标值。
具体实现步骤
- 记录进度条的当前值:在
circle-animation-progress事件回调中,将实时的stepValue存储到元素的data属性中,方便后续获取。 - 点击更新按钮时,先读取当前存储的进度值,作为
animationStartValue的参数,同时设置目标value,重新调用circleProgress方法。
修改后的完整代码
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-circle-progress/1.2.2/circle-progress.min.js"></script> <div class="wrapper"> <div class="card react"> <div class="circle"> <div class="bar"></div> <div class="box"><span></span></div> </div> <div class="text">React JS</div> </div> </div> <button id="add">Add</button>
CSS
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; } body{ display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; background: -webkit-linear-gradient(left, #53c68a, #8b67c4); } .wrapper{ width: 1000px; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; } .wrapper .card{ background: #fff; width: calc(25% - 8px); height: 300px; border-radius: 5px; display: flex; align-items: center; justify-content: space-evenly; flex-direction: column; box-shadow: 0px 10px 15px rgba(0,0,0,0.1); } .wrapper .card .circle{ position: relative; height: 150px; width: 150px; border-radius: 50%; cursor: default; } .card .circle .box, .card .circle .box span{ position: absolute; top: 50%; left: 50%; } .card .circle .box{ height: 100%; width: 100%; background: #fff; border-radius: 50%; transform: translate(-50%, -50%) scale(0.8); transition: all 0.2s; } .card .circle:hover .box{ transform: translate(-50%, -50%) scale(0.91); } .card .circle .box span, .wrapper .card .text{ background: -webkit-linear-gradient(left, #a445b2, #fa4299); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } .circle .box span{ font-size: 38px; font-family: sans-serif; font-weight: 600; transform: translate(-45%, -45%); transition: all 0.1s; } .card .circle:hover .box span{ transform: translate(-45%, -45%) scale(1.09); } .card .text{ font-size: 20px; font-weight: 600; } @media(max-width: 753px){ .wrapper{ max-width: 700px; } .wrapper .card{ width: calc(50% - 20px); margin-bottom: 20px; } } @media(max-width: 505px){ .wrapper{ max-width: 500px; } .wrapper .card{ width: 100%; } }
JavaScript
let options = { startAngle: -1.55, size: 150, value: 0.85, fill: {gradient: ['#a445b2', '#fa4299']} } // 初始化第一个进度条并记录当前值 $(".circle .bar").circleProgress(options).on('circle-animation-progress', function(event, progress, stepValue){ $(this).parent().find("span").text(String(stepValue.toFixed(2).substr(2)) + "%"); // 存储当前进度值到元素data属性 $(this).data('currentValue', stepValue); }); // 初始化React进度条并记录当前值 const $reactBar = $(".react .bar"); $reactBar.circleProgress({ value: 0.60, startAngle: -1.55, size: 150, fill: {gradient: ['#a445b2', '#fa4299']} }).on('circle-animation-progress', function(event, progress, stepValue){ $(this).parent().find("span").text(String(stepValue.toFixed(2).substr(2)) + "%"); $(this).data('currentValue', stepValue); }); // 点击按钮更新进度,实现平滑过渡 $('#add').on('click', function(){ const currentValue = $reactBar.data('currentValue') || 0; $reactBar.circleProgress({ value: 0.80, animationStartValue: currentValue, // 从当前值开始动画 startAngle: -1.55, size: 150, fill: {gradient: ['#a445b2', '#fa4299']} }).on('circle-animation-progress', function(event, progress, stepValue){ $(this).parent().find("span").text(String(stepValue.toFixed(2).substr(2)) + "%"); $(this).data('currentValue', stepValue); }); });
关键说明
animationStartValue是插件的内置参数,用于指定动画的起始进度值,默认值为0,修改为当前进度值后,动画就会从当前位置开始过渡。- 通过元素的
data('currentValue')存储实时进度值,确保每次更新都能获取到最新的当前值。 - 注意更新时要保持其他配置(如
startAngle、fill等)与初始化一致,避免样式或角度出现变化。
内容的提问来源于stack exchange,提问作者HiDayurie Dave
相关产品推荐
相关产品推荐

