javascript - Change a value over a set amount of time with jQuery? -
i'm working on project calls circular progress bar. found 1 trick here:
http://codepen.io/shankarcabus/pen/gzafb
but need animate on page load go in value each time:
<div class="progress-pie-chart" data-percent="43">
so on "page1.htm", need data-percent value automatically increase incrementally 0-20. on "page2.htm" 20-33, etc. i'm pretty new jquery, have no idea begin on that.
how create function increase data-percent value on say, 500 milliseconds?
using setinterval
can produce like. use math calculate steps based on fps create smooth animation. decimals can used percent
var start = 0; var end = 30; var time = 800; //in ms var fps = 30; var increment = ((end-start)/time)*fps; $('.progress-pie-chart')[0].dataset.percent = start; var timer = setinterval(function() { $('.progress-pie-chart')[0].dataset.percent = parsefloat($('.progress-pie-chart')[0].dataset.percent) + increment; if (parsefloat($('.progress-pie-chart')[0].dataset.percent) >= end) { clearinterval(timer); } var $ppc = $('.progress-pie-chart'), percent = parsefloat($ppc[0].dataset.percent), deg = 360 * percent / 100; if (percent > 50) { $ppc.addclass('gt-50'); } $('.ppc-progress-fill').css('transform', 'rotate(' + deg + 'deg)'); $('.ppc-percents span').html(parseint(percent, 10) + '%'); }, fps);
.progress-pie-chart { width: 200px; height: 200px; border-radius: 50%; background-color: #e5e5e5; position: relative; } .progress-pie-chart.gt-50 { background-color: #81ce97; } .ppc-progress { content: ""; position: absolute; border-radius: 50%; left: calc(50% - 100px); top: calc(50% - 100px); width: 200px; height: 200px; clip: rect(0, 200px, 200px, 100px); } .ppc-progress .ppc-progress-fill { content: ""; position: absolute; border-radius: 50%; left: calc(50% - 100px); top: calc(50% - 100px); width: 200px; height: 200px; clip: rect(0, 100px, 200px, 0); background: #81ce97; transform: rotate(60deg); } .gt-50 .ppc-progress { clip: rect(0, 100px, 200px, 0); } .gt-50 .ppc-progress .ppc-progress-fill { clip: rect(0, 200px, 200px, 100px); background: #e5e5e5; } .ppc-percents { content: ""; position: absolute; border-radius: 50%; left: calc(50% - 173.91304px/2); top: calc(50% - 173.91304px/2); width: 173.91304px; height: 173.91304px; background: #fff; text-align: center; display: table; } .ppc-percents span { display: block; font-size: 2.6em; font-weight: bold; color: #81ce97; } .pcc-percents-wrapper { display: table-cell; vertical-align: middle; } body { font-family: arial; background: #f7f7f7; } .progress-pie-chart { margin: 50px auto 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="progress-pie-chart" data-percent="0"> <div class="ppc-progress"> <div class="ppc-progress-fill"></div> </div> <div class="ppc-percents"> <div class="pcc-percents-wrapper"> <span>%</span> </div> </div> </div>
Comments
Post a Comment