html5 简单进度条,HTML5简单进度条插件

今天学习了HTML5画线条,于是有了做一个简单进度条的插件的想法 @H_502_8@ 先来一个实例 @H_502_8@ 下面是HTML代码

然后js配置参数

var setting = {

id: "canvas",//画布id 不可省略

width: 40,//进度条高度 可省略

time: 100,//进度刷新时间间隔 可省略 默认为1000毫秒

color: "black",//进度条颜色 可省略 默认为bule

runX2: function (x) {//每次刷新调用的改变X2坐标的函数,可省略 runX1,runY1,runY2一样

x = x + 2;

return x;

},endFunction: function () {//结束时调用的函数

},startFunction: function () {//开始时调用的函数

}

}

然后

initProgressBar(setting);

就行了 当然,最开始要引入自定义进度条的js

就这样 一个简单的HTML5 canvas实现的进度条就实现了 @H_502_8@ 我们先来说下API ,API很简单,只有几个参数,一般来说 只需要一个ID和一个变化函数就可以实现一个简单的进度条 @H_502_8@ id 画布的id @H_502_8@ width:进度条的高度,也可以叫宽度 默认10 @H_502_8@ time:刷新时间,也就是说每隔多久执行一次函数 默认1000毫秒 @H_502_8@ color:进度条的颜色 默认red @H_502_8@ lineCap:样式 和HTML5的lineCap样式一样 默认round @H_502_8@ runX2:每次刷新调用的变化函数,每次刷新调用的改变X2坐标的函数,runY2一样 共有两个坐标x1,y1,x2,y2 @H_502_8@ startFunction:开始之前调用的函数 @H_502_8@ endFunction:结束之后调用的函数 @H_502_8@ 是不是很简单?那我们现在来看下插件实现的源码,也很简单,不过100行。运用js的setInterval实现定时刷新。

function initProgressBar(setting) {

var initProgressBar = {};

//调用开始函数

if (setting.startFunction) {

setting.startFunction();

}

//初始化默认值函数

initProgressBar.initSet = function (set) {

if (!set.time) {

set.time = 1000;

}

if (!set.width) {

set.width = 10;

}

if (!set.color) {

set.color = "red";

}

if (!set.lineCap) {

set.lineCap = "round";

}

return set;

}

//初始化默认值

setting = initProgressBar.initSet(setting);

//刷新函数

initProgressBar.remainTime = function () {

if (setting.runX1) {

x1 = setting.runX1(x1);

if (x1 >= endW) {

x1 = endW;

}

}

if (setting.runY1) {

y1 = setting.runY1(y1);

if (y1 >= endH) {

y1 = endH;

}

}

if (setting.runX2) {

x2 = setting.runX2(x2);

if (x2 >= endW) {

x2 = endW;

}

}

if (setting.runY2) {

y2 = setting.runY2(y2);

if (y2 >= endH) {

y2 = endH;

}

}

initProgressBar.draw(x1,y2,setting.width,setting.lineCap,setting.color);

//判断结束

if (y2 >= endH || y1 >= endH || x1 >= endW || x2 >= endW) {

clearInterval(initProgressBar.run);

if (setting.endFunction) {

setting.endFunction();//调用结束函数

}

}

}

//初始化画布,调用刷新函数

if (setting.id) {

initProgressBar.canvas = document.getElementById(setting.id);

console.log(setting.id);

initProgressBar.context = initProgressBar.canvas.getContext("2d");

initProgressBar.run = setInterval(initProgressBar.remainTime,setting.time);//1000为1秒钟

} else {

alert("初始化错误,没有id");

}

var x1 = setting.width / 2;//起始点的坐标x

var y1 = setting.width / 2;//起始点的坐标y

var x2 = setting.width / 2;//结束点的坐标x

var y2 = setting.width / 2;//结束点的坐标y

var endH = initProgressBar.canvas.height - setting.width;

var endW = initProgressBar.canvas.width - setting.width;

//画进度条

initProgressBar.draw = function draw(x1,width,lineCap,color) {

//清除内容

initProgressBar.context.clearRect(0,0,canvas.width,canvas.height);

initProgressBar.context.beginPath();

initProgressBar.context.strokeStyle = color;

initProgressBar.context.lineCap = lineCap;

initProgressBar.context.moveTo(x1,y1);

initProgressBar.context.lineWidth = width;

initProgressBar.context.lineTo(x2,y2);

initProgressBar.context.stroke();

}

}

就这样,一个自定义小插件就完成了。 @H_502_8@在线演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常精美的h5 进度条 |DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库 <!----> .ClassyCountdownDemo { margin:0 auto 30px auto; max-width:800px; width:calc(100%); padding:30px; display:block } #countdown2 { background:#FFF } #countdown3 { background:rgb(52, 73, 94) } #countdown4 { background:#222 } #countdown5 { background:#222 } #countdown6 { background:#222 } #countdown7 { background:#222 } #countdown8 { background:#222 } #countdown9 { background:#FFF } #countdown10 { background:#3498db } jQuery炫酷图片预览Lightbox插件 A jQuery plugin designed to provide gallery view for images jQuery之家 返回下载页 Example $(document).ready(function() { $('#countdown15').ClassyCountdown({ theme: "flat-colors", end: $.now() + 10000 }); $('#countdown16').ClassyCountdown({ theme: "flat-colors-wide", end: $.now() + 10000 }); $('#countdown17').ClassyCountdown({ theme: "flat-colors-very-wide", end: $.now() + 10000 }); $('#countdown18').ClassyCountdown({ theme: "flat-colors-black", end: $.now() + 10000 }); $('#countdown1').ClassyCountdown({ theme: "white", end: $.now() + 645600 }); $('#countdown5').ClassyCountdown({ theme: "white", end: $.now() + 10000 }); $('#countdown6').ClassyCountdown({ theme: "white-wide", end: $.now() + 10000 }); $('#countdown7').ClassyCountdown({ theme: "white-very-wide", end: $.now() + 10000 }); $('#countdown8').ClassyCountdown({ theme: "white-black", end: $.now() + 10000 }); $('#countdown11').ClassyCountdown({ theme: "black", style: { secondsElement: { gauge: { fgColor: "#F00" } } }, end: $.now() + 10000 }); $('#countdown12').ClassyCountdown({ theme: "black-wide", labels: false, end: $.now() + 10000 }); $('#countdown13').ClassyCountdown({ theme: "black-very-wide", labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown14').ClassyCountdown({ theme: "black-black", labelsOptions: { style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown4').ClassyCountdown({ end: $.now() + 10000, labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, hours: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, minutes: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, seconds: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown2').ClassyCountdown({ end: '1388468325', now: '1378441323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown9').ClassyCountdown({ end: '1388468325', now: '1380501323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#1abc9c", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#2980b9", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#8e44ad", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#f39c12", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown10').ClassyCountdown({ end: '1397468325', now: '1388471324', labels: true, labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown3').ClassyCountdown({ end: '1390868325', now: '1388461323', labels: true, labelsOptions: { lang: { days: 'Zile', hours: 'Ore', minutes: 'Minute', seconds: 'Secunde' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' } }, onEndCallback: function() { console.log("Time out!"); } }); }); 如果你喜欢这个插件,那么你可能也喜欢: html5+jquery通过鼠标控制的圆形进度条 jQuery和css3旋钮控制按钮-knobKnob

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值