原生JS简单绘制一个圆形进度条
2018-10-23 13:28
525 查看
结构:
[code]<div class="box"></div>
样式
[code]/*原形条*/ .wrap,.circle,.num {width: 200px;height: 200px;border-radius: 50%;} .wrap { position: relative; border: 10px solid #ccc;box-sizing: border-box;} .circle { position: absolute; top: -10px; left: -10px; box-sizing: border-box;} .circle div {width: 200px; height: 200px;position: absolute;box-sizing: border-box; border-radius: 50%;} .num { position: absolute; top: -10px; left: -10px; line-height: 200px; text-align: center; font-size: 50px;} .left { clip: rect(0, 100px, 200px, 0); border: 10px solid #f00;} .right {clip: rect(0, 200px, 200px, 100px); border: 10px solid #f00;}
JS代码
[code] function Yuan(element,num){ this.box = element;//获取节点 this.box.className = 'wrap'; //创建节点circle this.circle = document.createElement("div"); this.circle.className = "circle"; this.box.appendChild(this.circle); //创建circle的子节点left this.left = document.createElement("div"); this.left.className = "left"; this.circle.appendChild(this.left); //创建circle的子节点right this.right = document.createElement("div"); this.right.className = "right"; this.circle.appendChild(this.right); //创建节点num this.num = document.createElement("div"); this.num.className = "num"; this.box.appendChild(this.num); this.num.innerHTML = '<span class="">'+ num +'</span>%'; this.jz(num); } Yuan.prototype.jz = function(num){ var i = 0; var that = this; var int = setInterval(function(){ that.gx( num/10*i ); i++; if(i>10){ clearInterval(int) } },0);//这里可以修改绘制进度的时间 } Yuan.prototype.gx = function(n){ if(n <= 50) { this.right.style.display = 'none'; this.circle.style.clip = 'rect(0, 200px, 200px, 100px)'; } else { this.right.style.display = 'block'; this.circle.style.clip = 'rect(auto, auto, auto, auto)'; } this.left.style.transform = 'rotate(' + n * 360 / 100 + 'deg)'; } let box = new Yuan(document.querySelector('.box'),78);
转载自https://blog.csdn.net/cjp66666666666/article/details/80594664
实现原理链接:点我啦~~
阅读更多相关文章推荐
- 如何用原生JS实现一个简单的promise
- HTML5_canvas(设置画笔样式、左右跑动的小块、绘制路径的方法、写字板、绘制圆形的方法、吃豆豆、绘制文本、环形进度条)设置一个模板
- 用原生JS实现一个简单计算的功能实例
- COCOS2D-X之圆形进度条的一个简单Demo
- 使用原生js写的一个简单slider
- COCOS2D-X之圆形进度条的一个简单Demo
- Vue.js如何写一个简单的原生js模块,浏览器中的表现如何?
- [置顶] JS简单原生-定义一个创建HTML列表的方法
- 原生js实现Canvas实现拖拽式绘图,支持画笔、线条、箭头、三角形和圆形等等图形绘制功能,有实例Demo
- 用js写一个简单的进度条
- vue.js如何写一个简单的原生js模块,浏览器中的表现如何?
- 使用原生js写的一个简单slider
- 用原生JS实现一个简单的计时器demo
- 原生js写的一个简单slider
- js写一个简单的进度条加载
- 一种将图片绘制在一个圆形区域内的简单方法——CGContextClip
- jquery.flot.js简单绘制折线图用法示例
- 一个简单js的拼参数
- 原生JS实现图片轮播与淡入效果的简单实例
- 原生js在IE7下 向dom添加节点的一个bug, (本例为添加hidden input)