jquery 圆形进度条

最近手里面的项目需要完成这个对设备性能的检测显示功能,需要使用到圆形进度条这样的效果,网上找了一圈,有很多相当的插件,找到:circliful 插件,看了他的使用说明比较的方便,于是就下载了它并将自己想要的效果添加了进去;

上面的效果,需要圆心中间的数字动态展示,并且在颜色值在小于50的时候为绿色,大于50的时候为红色;

第一步: 页面布局

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>圆形进度条</title>
<link rel="icon" href="images/50.gif" type="image/gif" />
<script src="js/jquery.min.js"></script>
<script src="js/jquery.circliful.js"></script>
<style>
	body{ margin:0; padding:0; font-size:14px; line-height:24px; color:#8b8b8b; font-family:Microsoft YaHei, Arial, Helvetica, sans-serif;}
	.you{ margin-left:36px; float:left; height:auto; width:55%; min-width:500px}
	.yous{ box-shadow:0px 0px 5px  #ddd; background:#FFF;}
	.youz{ background:url(./images/tbg.png) repeat-x; height:39px; line-height:39px; font-size:14px; color:#323436; text-indent:20px}
	.rrlh{ height:180px; margin-top:20px}
	.txz{ position: absolute; left: 50%; top: 50%; margin-left: -30px; margin-top: -30px; line-height: 30px;}
	.cpu{ width:50%; height:170px; float:left; text-align:center; position:relative}
</style>
</head>
<div class="you">
	<div class="yous">		 
		<div class="youz">运行状态</div>		 
		<div class="rrlh">
			<!-- cpu使用率 -->
			<div class="cpu">
				<div class="txz"><span style="font-size: 40px;" id="cpuText">0</span><br /> CPU使用率</div>
				<div id="myStat2" style="display: inline-block" data-width="10" data-fontsize="28" ></div>
			</div>
			<!-- 内存使用率 -->
			<div class="cpu">
				<div class="txz"><span style="font-size: 40px;" id="ncText">0</span><br /> 内存使用率</div>
				<div id="myStat3" style="display: inline-block" data-width="10" data-fontsize="28" ></div>
			</div>
		</div>
	</div>		 
</div>
</html>

第二步: 修改 jquery.circliful.js

(function( $ ) {
    $.fn.circliful = function(options) {
        var settings = $.extend({
            // These are the defaults.
            foregroundColor: "#556b2f",
            backgroundColor: "#eee",
            fillColor: false,
            width: 15,
            dimension: 200,
            size: 15, 
			percent: 50,
			endPercent: 100,
			showValue: "showValue", //新增 动态数值变化值显示标签ID
            animationStep: 1.0
        }, options );
         return this.each(function() {
                var dimension = ‘‘;
                var text = ‘‘;
				var info = ‘‘;
                var width = ‘‘;
                var size = 0;
				var percent = 0;
				var endPercent = 100;
				var fgcolor = ‘‘;
				var bgcolor = ‘‘;
				var icon = ‘‘;
                var animationstep = 0.0;
				var showValue = ‘‘;
                $(this).addClass(‘circliful‘);

				showValue = settings.showValue; //赋值

                if($(this).data(‘dimension‘) != undefined) {
                    dimension = $(this).data(‘dimension‘);
                } else {
                    dimension = settings.dimension;
                }
    
                if($(this).data(‘width‘) != undefined) {
                    width = $(this).data(‘width‘);
                } else {
                    width = settings.width;
                }
    
                if($(this).data(‘fontsize‘) != undefined) {
                    size = $(this).data(‘fontsize‘);
                } else {
                    size = settings.size;
                }

				if($(this).data(‘percent‘) != undefined) {
                    percent = $(this).data(‘percent‘) / 100;
					endPercent = $(this).data(‘percent‘);
                } else {
                    percent = settings.percent / 100;
					endPercent = settings.endPercent;
                }

				if($(this).data(‘fgcolor‘) != undefined) {
                    fgcolor = $(this).data(‘fgcolor‘);
                } else {
                    fgcolor = settings.foregroundColor;
                }

				if($(this).data(‘bgcolor‘) != undefined) {
                    bgcolor = $(this).data(‘bgcolor‘);
                } else {
                    bgcolor = settings.backgroundColor;
                }

                if($(this).data(‘animation-step‘) != undefined) {
                    animationstep = parseFloat($(this).data(‘animation-step‘));
                } else {
                    animationstep = settings.animationStep;
                }
                if($(this).data(‘text‘) != undefined) {
                    text = $(this).data(‘text‘);

					if($(this).data(‘icon‘) != undefined) {
						icon = ‘<i class="fa ‘ + $(this).data(‘icon‘) + ‘"></i>‘;
					}

					 if($(this).data(‘type‘) != undefined) {
						type = $(this).data(‘type‘);

						if(type == ‘half‘) {
							$(this).append(‘<span class="circle-text-half">‘ +  icon  + text + ‘</span>‘);
							$(this).find(‘.circle-text-half‘).css({‘line-height‘: (dimension / 1.45) + ‘px‘, ‘font-size‘ : size + ‘px‘ });
						} else {
							$(this).append(‘<span class="circle-text">‘ + icon + text + ‘</span>‘);
							//设置文字样式
							$(this).find(‘.circle-text‘).css(
								{‘line-height‘: dimension + ‘px‘, ‘font-size‘ : size + ‘px‘}
							);
						}
					} else {
						$(this).append(‘<span class="circle-text">‘ + icon + text + ‘</span>‘);
						$(this).find(‘.circle-text‘).css(
							{‘line-height‘: dimension + ‘px‘, ‘font-size‘ : size + ‘px‘ }
						);
					}
                } else if($(this).data(‘icon‘) != undefined) {

				}

				if($(this).data(‘info‘) != undefined) {
                    info = $(this).data(‘info‘);

					if($(this).data(‘type‘) != undefined) {
						type = $(this).data(‘type‘);

						if(type == ‘half‘) { 
							$(this).append(‘<span class="circle-info-half">‘ + info + ‘</span>‘);
							$(this).find(‘.circle-info-half‘).css({‘line-height‘: (dimension * 0.9) + ‘px‘, });
						} else {
							$(this).append(‘<span class="circle-info">‘ + info + ‘</span>‘);
							$(this).find(‘.circle-info‘).css({‘line-height‘: (dimension * 1.25) + ‘px‘, });
						}
					} else {
						$(this).append(‘<span class="circle-info">‘ + info + ‘</span>‘);
						$(this).find(‘.circle-info‘).css({‘line-height‘: (dimension * 1.25) + ‘px‘, });
					}
                }
    
                $(this).width(dimension + ‘px‘);

              var canvas = $(‘<canvas></canvas>‘).attr({ width: dimension, height: dimension }).appendTo($(this)).get(0);
              var context = canvas.getContext(‘2d‘);
              var x = canvas.width / 2;
              var y = canvas.height / 2;
			  var degrees = percent * 360.0;
			  var radians = degrees * (Math.PI / 180);
              var radius = canvas.width / 2.5;
              var startAngle = 2.3 * Math.PI;
              var endAngle = 0;
              var counterClockwise = false;
              var curPerc = animationstep === 0.0 ? endPercent : 0.0;
              var curStep = Math.max(animationstep, 0.0);
			  var circ = Math.PI * 2;
			  var quart = Math.PI / 2;
			  var type = ‘‘;
			  var fill = false;
			  
			  if($(this).data(‘type‘) != undefined) {
                    type = $(this).data(‘type‘);

					if(type == ‘half‘) {
						var startAngle = 2.0 * Math.PI;
						var endAngle = 3.13;
						var circ = Math.PI * 1.0;
						var quart = Math.PI / 0.996;
					}
                }

				if($(this).data(‘fill‘) != undefined) {
					fill = $(this).data(‘fill‘);
				} else {
					fill = settings.fillColor;
				}
			  //animate foreground circle
			  function animate(current) {
				/**
				 * [修改] 设置圆心动态数据变化值
				 * showValue 为显示动态值的html标签的ID
				 * 这里 parseInt(current*100) 取整数,他的最大值为 endPercent的值
				 **/
				$("#"+showValue).html(parseInt(current*100));
				/**
				 * [修改] 判断值是否超过圆形的一半,并修改圆形颜色				 *
				 **/
				if(current < 0.5){
					fgcolor = ‘#14b997‘;
				}else{
					fgcolor = ‘#f75656‘;
				}

				context.clearRect(0, 0, canvas.width, canvas.height);				 
				context.beginPath();
				context.arc(x, y, radius, endAngle, startAngle, false);
				context.lineWidth = width - 1;
				// line color
				context.strokeStyle = bgcolor;
				context.stroke();
				if(fill) {
					context.fillStyle = fill;
					context.fill();
				}
				context.beginPath();
				context.arc(x, y, radius, -(quart), ((circ) * current) - quart, false);
				context.lineWidth = width;
				// line color
				context.strokeStyle = fgcolor;
				context.stroke();

				if (curPerc < endPercent) {
  				     curPerc += curStep;
					 requestAnimationFrame(function () {
						/**
						 * [修改] 降低圆形进度条速度
						 **/
						setTimeout(function(){
							animate(Math.min(curPerc, endPercent) / 100);
						}, 40);
					 });
				}
			 }
			 animate(curPerc / 100);
        });
    };
}( jQuery ));

第三步: 调用

$(document).ready(function(){
		var cupCount = 10,
			ncCount = 80;
		var myStat2Color = cupCount > 50 ? ‘#f75656‘ : ‘#14b997‘;
		var myStat3Color = ncCount > 50 ? ‘#f75656‘ : ‘#14b997‘;
		$(‘#myStat2‘).circliful({
			dimension: 170,
			endPercent: cupCount,
			showValue: "cpuText",
			foregroundColor: myStat2Color,//556b2f
			backgroundColor: "#eee",
		});
		$(‘#myStat3‘).circliful({
			dimension: 170,
			endPercent: ncCount,
			showValue: "ncText",
			foregroundColor: myStat3Color,
			backgroundColor: "#eee",
		});
	});

注: 插件下载 https://github.com/pguso/jquery-plugin-circliful   按需修改,如果不妥或者其他,联系:594710017。

插件参数说明

时间: 2024-08-06 06:52:40

jquery 圆形进度条的相关文章

基于jQuery漂亮的圆形进度条倒计时插件

jQuery Final Countdown是一款时尚的圆形进度条样式的jQuery倒计时插件.该倒计时插件可以显示倒计时的秒.分钟.小时和天数.它采用圆形进度条来作为倒计时的动画,非常的时尚大方. 在线预览   源码下载 使用方法 该倒计时插件依赖于jQuery和KineticJS-一个HTML5 Canvas库.使用是要将它们引入. <script type="text/javascript" src="js/jquery.min.js"></

利用css3动画和border来实现圆形进度条

最近在学习前端的一些知识,发现border的功能十分强大啊! 首先来看看demo 就是这么一个圆形的进度条,在文本框中输入0-100的数值下面的进度条相应的转到多少 这个主要是利用border,旋转和css动画来实现的,主要思想是利用两个div来互相遮挡border形成的一个只有半圈有颜色的圆形,再利用旋转div的角度来调整显示 上代码: html+css+js(这里引入了jquery) <!DOCTYPE html> <html lang="en"> <

HTML5 canvas带渐变色的圆形进度条动画

query-circle-progress是一款带渐变色的圆形进度条动画特效jQuery插件.该圆形进度条使用的是HTML5 canvas来绘制圆形进度条及其动画效果,进度条使用渐变色来填充,效果非常的酷. 效果演示:http://www.htmleaf.com/Demo/201505271919.html 下载地址:http://www.htmleaf.com/html5/html5-canvas/201505271918.html

Highcharts 测量图;Highcharts 圆形进度条式测量图;Highcharts 时钟;Highcharts 双轴车速表;Highcharts 音量表(VU Meter)

Highcharts 测量图 配置 chart.type 配置 配置 chart 的 type 为 'gauge' .chart.type 描述了图表类型.默认值为 "line". var chart = { type: 'guage' }; pane 配置 pane 只适用在极坐标图和角度测量仪.此可配置对象持有组合x轴和y周的常规选项.每个x轴和y轴都可以通过索引关联到窗格中. var pane = { startAngle: -150, // x轴或测量轴的开始度数,以度数的方式

自定义圆形进度条

关于控件呢,我想大家应该都很熟悉了吧,android应用开发MVC架构中,控件担任着至关重要的作用,感觉可以说是基于控件的事件模型人机交互的基础吧.这种特性感觉在wpf开发中体现得更为直接,感兴趣的同学可以去了解一下.而android框架自身就已经给我们提供了很多控件.那么问题来了?为什么有那么多控件可以用,你还要去屑自定义控件呢?是因为大家闲的蛋疼吗?显然不是.个人认为只要有两方面吧,要么是觉得有些原生控件是在是丑得难以忍受(即使是在你已经自定义了他的shape,圆角,selector等一系列

Android自定义View——圆形进度条式按钮

介绍 今天上班的时候有个哥们问我怎么去实现一个按钮式的进度条,先来看看他需要实现的效果图. 和普通的圆形进度条类似,只是中间的地方有两个状态表示,未开始,暂停状态.而且他说圆形进度的功能已经实现了.那么我们只需要对中间的两个状态做处理就行了. 先来看看实现的效果图: 上面说了我们只需要处理中间状态的变化就可以了,对于进度的处理直接使用了弘洋文章中实现: http://blog.csdn.net/lmj623565791/article/details/43371299 下面开始具体实现. 具体实

JQuery入门——进度条

越来越觉得常规javascript已经跟不上节奏了,打算学点进阶的,从JQuery学起. JQuery是一个Javascript库,可以从JQuery.com下载,放到本地,用 <script src="jquery.js"></script> 语句来引用.如果不想下载jquery,可以引用web上的jquery库,从google或微软的服务器上引用他们的jquery库. 引用google的jquery库: <script src="http:/

Html5基于SVG的扁平风格圆形进度条javascript插件教程

一.使用方法 使用该圆形进度条需要引入circleDonutChart.js文件. <script type="text/javascript" src="circleDonutChart.js"></script> 二.Html结构 你可以使用一个空的<div>元素来制作圆形进度条. <div id="example1"></div> 三.初始化插件 要制作圆形进度条,可以使用下面的方

Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

转载请注明地址:http://blog.csdn.net/xiaanming/article/details/10298163 很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为了我们自定义的View在一个项目中能够重用,有时候我们需要自定义其属性,举个很简单的例子,我在项目中的多个界面使用我自定义的View,每个界面该自定义View