在负责的项目中,有一个环形渐变读取进度的效果的需求,于是在网上查阅相关资料整理一下。
代码如下:
<!doctype html><html><head> <meta charset="utf-8"> <title>title</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script></head><body> <div> <svg width="340" height="340" xmlns="http://www.w3.org/2000/svg"> <circle r="150" cy="170" cx="170" stroke-width="10" stroke="#fafafa" fill="none" /> <circle r="150" cy="170" cx="170" stroke-width="35" stroke="url(#lineargradient)" stroke-linejoin="round" stroke-linecap="round" fill="none"/> <circle fill="#ffffff" fill-rule="nonzero" cx="320" cy="170" r="12"></circle> <defs> <lineargradient x1="0%" y1="80.9878035%" x2="96.9769965%" y2="41.7547666%" id="lineargradient"> <stop stop-color="#18a6ff" offset="0%"></stop> <stop stop-color="#32b7ff" offset="52.7095376%"></stop> <stop stop-color="#0094f1" offset="100%"></stop> </lineargradient> </defs> </svg> <div> <div> <span></span><span>%</span> </div> </div> </div> <script type="text/javascript"> window.onload=function () { 'use strict'; var progresscircle = (function () { function progresscircle(percent,radius,elementclass){ this._percent = percent; //百分比 this._radius = radius; //圆的半径 this._elementclass = elementclass; } progresscircle.prototype.calcdashoffset = function () { var circumference; circumference = math.pi * (2 * this._radius); //计算圆圈的周长 return math.round(circumference - this._percent / 100 * circumference); //计算圆圈要渲染的 长度! } //渲染进度条 progresscircle.prototype.createcss = function() { return $(. + this._elementclass + .circle_bar).css('stroke-dashoffset', this.calcdashoffset()); }; //读取效果 progresscircle.prototype.updatetext = function () { $(. + this._elementclass + .js-donut-figure)[0].innertext = this._percent; } progresscircle.prototype.init = function() { var _this = this; settimeout(function(){ _this.updatetext(); return _this.createcss(); },1000); }; return progresscircle; })(); let progress = new progresscircle(50, 150, 'donut'); progress.init(); } </script> <style type="text/css"> *{ padding:0; margin:0 } .donut{ position: relative; } .circle_warp{ position: relative; top: 0; left: 0 } .circle_bar { stroke-dasharray: 942.4777960769379; //计算整个圆周的周长公式为circumstance=2*pi*radius 2*3.14*半径(此时是半径是150) stroke-dashoffset: 942.4777960769379; transition: stroke-dashoffset 1200ms cubic-bezier(.99,.01,.62,.94); } .donut_svg{ transform: rotate(-90deg); } .donut_copy{ text-align: center; width: 340px; height: 340px; top: 40%; left: 0; position: absolute; } .donut_title{ opacity: 0; font-size: 42px; color: #171717; margin-bottom: 2px; animation: donuttitlefadeleft 800ms 200ms cubic-bezier(.99,.01,.22,.94) forwards; transform: translatex(0); font-weight: bold; } .donut_spic{ content: %; animation: donuttitlefaderight 800ms 200ms cubic-bezier(.99,.01,.22,.94) forwards; opacity: 0; transform: translatey(-20px); } .donut__text p{ font-size: 16px; color: #aaaaaa; } @keyframes donuttitlefadeleft { from { opacity: 0; transform: translatex(0); } to { opacity: 1; transform: translatex(10px); } } @keyframes donuttitlefaderight { from { opacity: 0; transform: translatex(-30px); } to { opacity: 1; transform: translatex(0); } }</style></html>
以上就是svg和css3实现环形渐变进度条的代码示例的详细内容。
