jquery插件canvaspercent.js實(shí)現(xiàn)百分比圓餅效果
在儀表盤(pán)的任務(wù)增多同時(shí),列表頁(yè)的百分比圓環(huán)或圓餅也隨之增多,于是順手在儀表盤(pán)的基礎(chǔ)上,封裝了一個(gè)小小的jquery插件(jq-canvaspercent.js),暫且版本1.0吧,到以后業(yè)務(wù)的變化在對(duì)其進(jìn)行功能拓展;
暫時(shí)性用于頁(yè)面中有多處百分比圓環(huán)的效果處理,還是不錯(cuò)的。
jq-canvaspercent.js代碼比較簡(jiǎn)單,以下直接給出插件代碼和幾張截圖:
/*
* canvaspercent 0.1
* Copyright:HeavyShell
* Date: 2016-06-27
* 利用canvas繪圖實(shí)現(xiàn)百分比percent圓餅圖
*/
(function($){
$.fn.drawCanvasPercent = function(options){
//各種屬性、參數(shù)
var defaults = {
type:1, //類(lèi)型默認(rèn)1,有[1,2,3]
dw:'rem', //判斷是單位是rem還是px
cir_r:1, //圓餅的直徑
cir_color:'#0e9cfa', //圓餅的占比顏色
cir_color_op:'#e0ebf4', //圓餅的占比顏色
line_w:0.16, //圓餅的線條寬度
fill_color:"#fff" //圓餅的中間區(qū)域填充顏色
}
var options = $.extend(defaults, options);
this.each(function(){
//插件實(shí)現(xiàn)代碼
var cur_obj=$(this);
if(options.dw=="rem"){
var cur_cir_r=options.cir_r*(window.screen.width/10);
var cur_line_w=options.line_w*(window.screen.width/10);
}else{
var cur_cir_r=options.cir_r;
var cur_line_w=options.line_w;
}
var cur_type=options.type;
var cur_cir_color=options.cir_color;
var cur_cir_color_op=options.cir_color_op;
var cur_fill_color=options.fill_color;
var percent=cur_obj.attr('data-percent');
cur_obj.attr({'width':cur_cir_r,'height':cur_cir_r});
cur_obj.css({'border-radius':"50%",'background':cur_cir_color_op});
if(cur_obj[0].getContext){
if(cur_type==2){
//無(wú)填充顏色,且線條寬度等于直徑
cur_line_w=cur_cir_r;
}else if(cur_type==3){
//無(wú)填充顏色
}else{
//有填充顏色
var ctx2 = cur_obj[0].getContext("2d");
ctx2.fillStyle = cur_fill_color;
ctx2.arc(cur_cir_r/2, cur_cir_r/2, cur_cir_r/2-cur_line_w/2, 0, Math.PI*2, false);
ctx2.fill();
}
var ctx = cur_obj[0].getContext("2d");
ctx.beginPath();
ctx.strokeStyle = cur_cir_color;
ctx.lineWidth=cur_line_w;
ctx.arc(cur_cir_r/2, cur_cir_r/2, cur_cir_r/2, 0, Math.PI*(percent/100)*360/180, false);
ctx.stroke();
}
});
};
})(jQuery);
調(diào)用方式:
$(function(){
$('.perCanvas').drawCanvasPercent();
});
也給出html頁(yè)面代碼吧:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<title>demo01</title>
<style type="text/css">
div{margin:.1rem .2rem;background:#eee;padding:.3rem}
div span{display:block;float:right;margin:.22rem 2rem 0 0;font-size:.4rem;font-family:microsoft yahei}
div canvas{
-webkit-transform: rotateZ(-270deg);
transform:rotateZ(-270deg);
-webkit-animation:ani01 1s ease 0s both;
animation:ani01 1s ease 0s both;
}
@-webkit-keyframes ani01 {
0%{
-webkit-transform:scale(.5,.5) rotateZ(-270deg);
transform:scale(.5,.5) rotateZ(-270deg);
}
100%{
-webkit-transform:scale(1,1) rotateZ(-90deg);
transform:scale(1,1) rotateZ(-90deg);
}
}
@keyframes ani01 {
0%{
-webkit-transform:scale(.5,.5) rotateZ(-270deg);
transform:scale(.5,.5) rotateZ(-270deg);
}
100%{
-webkit-transform:scale(1,1) rotateZ(-90deg);
transform:scale(1,1) rotateZ(-90deg);
}
}
</style>
</head>
<body>
<div>
<canvas data-percent="80" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 80%</span>
</div>
<div>
<canvas data-percent="50" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 50%</span>
</div>
<div>
<canvas data-percent="75" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 75%</span>
</div>
<div>
<canvas data-percent="35" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 35%</span>
</div>
<div>
<canvas data-percent="95" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 95%</span>
</div>
<div>
<canvas data-percent="13" class="perCanvas">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
<span>第一章:進(jìn)度 13%</span>
</div>
<script type="text/javascript" src="js/flexible.js"></script>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jq-canvaspercent.js"></script>
<script type="text/javascript">
$(function(){
$('.perCanvas').drawCanvasPercent();
});
</script>
</body>
</html>
截圖如下:



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js canvas仿支付寶芝麻信用分儀表盤(pán)
- ECharts儀表盤(pán)實(shí)例代碼(附源碼下載)
- Javascript highcharts 餅圖顯示數(shù)量和百分比實(shí)例代碼
- JavaScript根據(jù)數(shù)據(jù)生成百分比圖和柱狀圖的實(shí)例代碼
- 使用javascript獲取flash加載的百分比的實(shí)現(xiàn)代碼
- javascript 計(jì)算兩個(gè)整數(shù)的百分比值
- javascript下正則匹配百分比的代碼
- js canvas實(shí)現(xiàn)適用于移動(dòng)端的百分比儀表盤(pán)dashboard
相關(guān)文章
jQuery事件綁定on()與彈窗實(shí)現(xiàn)代碼
這篇文章主要介紹了jQuery事件綁定on()與彈窗實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-04-04
利用JQuery寫(xiě)一個(gè)簡(jiǎn)單的異步分頁(yè)插件
這篇文章主要為大家詳細(xì)介紹了如何利用JQuery寫(xiě)一個(gè)簡(jiǎn)單的異步分頁(yè)插件,感興趣的小伙伴們可以參考一下2016-03-03
bootstrap與Jquery UI 按鈕樣式?jīng)_突的解決辦法
這篇文章主要介紹了bootstrap與Jquery UI 按鈕樣式?jīng)_突的解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
seajs加載jquery時(shí)提示$ is not a function該怎么解決
這篇文章主要介紹了seajs加載jquery時(shí)提示$ is not a function該怎么解決的相關(guān)資料,需要的朋友可以參考下2015-10-10
最簡(jiǎn)單的jQuery程序 入門(mén)者學(xué)習(xí)
用jQuery寫(xiě)的一個(gè)簡(jiǎn)單的程序,用于入門(mén)練習(xí),發(fā)給大家,希望初學(xué)者有用.2009-07-07
基于jQuery的select下拉框選擇觸發(fā)事件實(shí)例分析
這篇文章主要介紹了基于jQuery的select下拉框選擇觸發(fā)事件實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了常用瀏覽器對(duì)select觸發(fā)事件的兼容及實(shí)現(xiàn)觸發(fā)的相關(guān)技巧,需要的朋友可以參考下2016-11-11
jQuery實(shí)現(xiàn)的彈幕效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的彈幕效果,結(jié)合完整實(shí)例形式分析了jQuery通過(guò)結(jié)合時(shí)間函數(shù)控制輸入文字與樣式的漸變實(shí)現(xiàn)彈幕效果,需要的朋友可以參考下2017-09-09
基于JQuery的多標(biāo)簽實(shí)現(xiàn)代碼
最近在學(xué)習(xí)JQuery,其實(shí)也不叫學(xué)習(xí),很久以前就學(xué)過(guò),只是有一段時(shí)間沒(méi)用,有些生疏了,于是就做幾個(gè)小例子來(lái)練習(xí)練習(xí),為了方便以后查找就將這些小示例記錄下來(lái)2012-09-09

