Html5实现loading功能

 Html5在移动设备上表现抢眼,几乎所有稍微高端一点的设备(乔帮主的iPad,iPhone和Andriod的平板手机等)的浏览器都支持Html5。而且据我个人的测试这些支持html5的设备对canvas标签的支持是相当的好。

所以就看了一点html5的东东,并研究了一下canvas标签的用法。如果你从来没有用过canvas标签,请参考Html5 canvas cheat sheet。

大家都知道web2.0以来大量的使用ajax,loading的小图标也有很多很多种,甚至还有专门提供loading图片的网站。所以我就想能不能让html5解决一下这个以前用gif文件解决的问题。没想到非常的简单,只用了不到一小时的时间就搞定了两个,而且这样做出来的loading图标是可定制的,既可以定制颜色,也可以定制大小等属性。

废话少叙,我们先来看下这两个loading图标的效果,注意,如果你用的是PC,请使用Firefox,Opera或者IE9看此效果。


第一个带着小尾巴转动的loading图标画图的思路是,首先画一个圆,然后在圆的边上按顺序画大小逐渐减小的小圆点,在每次刷新画布时改变这一系列的小圆点在大圆边上的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!
doctype
 
html>
<
html
>
  
<
head
>
    
<
meta
 
http-equiv="content-type" content="GBK"/>
    
<
title
>loading</
title
>
    
<
script
 
type="text/javascript">
   
    
function loading(canvas,options){
      
this.canvas = canvas;
      
if(options){
        
this.radius = options.radius||12;
        
this.circleLineWidth = options.circleLineWidth||4;
        
this.circleColor = options.circleColor||'lightgray';
        
this.dotColor = options.dotColor||'gray';
      
}else{     
        
this.radius = 12;
        
this.circelLineWidth = 4;
        
this.circleColor = 'lightgray';
        
this.dotColor = 'gray';
      
}
    
}
    
loading.prototype = {
      
show:function (){
        
var canvas = this.canvas;
        
if(!canvas.getContext)return;
        
if(canvas.__loading)return;
        
canvas.__loading = this;
        
var ctx = canvas.getContext('2d');
        
var radius = this.radius;     
        
var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];     
        
var me = this;
        
canvas.loadingInterval = setInterval(function(){
          
ctx.clearRect(0,0,canvas.width,canvas.height);        
          
var lineWidth = me.circleLineWidth;
          
var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};         
          
ctx.beginPath();
          
ctx.lineWidth = lineWidth;
          
ctx.strokeStyle = me.circleColor;
          
ctx.arc(center.x,center.y,radius,0,Math.PI*2);
          
ctx.closePath();
          
ctx.stroke();
          
for(var i=0;i<
rotators.length
;i  ){       
            
var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;           
            
//在圆圈上面画小圆
            
var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};           
            
var rotatorRadius = rotators[i].radius;
            
ctx.beginPath();
            
ctx.fillStyle = me.dotColor;
            
ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);
            
ctx.closePath();
            
ctx.fill();
            
rotators[i].currentAngle = rotatorAngle 4/radius;
          
}
        
},50);
      
},
      
hide:function(){
        
var canvas = this.canvas;
        
canvas.__loading = false;
        
if(canvas.loadingInterval){
          
window.clearInterval(canvas.loadingInterval);
        
}
        
var ctx = canvas.getContext('2d');
        
if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
      
}
    
};
    
 
    
</script>
  
</
head
>
  
<
body
>
    
<
canvas
 
id="canvas" width="300" height="100" style="border:1px solid #69c"></
canvas
>
    
<
p
>
    
<
input
 
type="button" οnclick="loadingObj.hide()" value="HideLoading"/>
    
<
input
 
type="button" οnclick="loadingObj.show()" value="showLoading"/>
    
</
p
>
    
<
script
>
    
var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
    
loadingObj.show();
    
</
script
>
  
</
body
>
</
html
>
,在一个圆环上有一个相同圆心相同半径的圆弧在不停的转动。画图的步骤是首先画一个圆环,然后画一个不同颜色相同圆心半径的圆弧,在每次刷新画布时改变圆弧的起始角度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!
doctype
 
html>
<
html
>
<
head
>
  
<
meta
 
http-equiv="content-type" content="text/html;charset=gbk"/>
  
<
title
>loading</
title
>
  
<
script
>
    
function loading(canvas,options){
      
this.canvas = canvas;
      
if(options){
        
this.radius = options.radius||12;
        
this.circleLineWidth = options.circleLineWidth||4;
        
this.circleColor = options.circleColor||'lightgray';
        
this.moveArcColor = options.moveArcColor||'gray';
      
}else{     
        
this.radius = 12;
        
this.circelLineWidth = 4;
        
this.circleColor = 'lightgray';
        
this.moveArcColor = 'gray';
      
}
    
}
    
loading.prototype = {
      
show:function (){
        
var canvas = this.canvas;
        
if(!canvas.getContext)return;
        
if(canvas.__loading)return;
        
canvas.__loading = this;
        
var ctx = canvas.getContext('2d');
        
var radius = this.radius;     
        
var me = this;
        
var rotatorAngle = Math.PI*1.5;
        
var step = Math.PI/6;
        
canvas.loadingInterval = setInterval(function(){
          
ctx.clearRect(0,0,canvas.width,canvas.height);        
          
var lineWidth = me.circleLineWidth;
          
var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};         
          
ctx.beginPath();
          
ctx.lineWidth = lineWidth;
          
ctx.strokeStyle = me.circleColor;
          
ctx.arc(center.x,center.y,radius,0,Math.PI*2);
          
ctx.closePath();
          
ctx.stroke();
          
//在圆圈上面画小圆
          
ctx.beginPath();
          
ctx.strokeStyle = me.moveArcColor;
          
ctx.arc(center.x,center.y,radius,rotatorAngle,rotatorAngle Math.PI*.45);
          
ctx.stroke();
          
rotatorAngle =step;
         
 
        
},50);
      
},
      
hide:function(){
        
var canvas = this.canvas;
        
canvas.__loading = false;
        
if(canvas.loadingInterval){
          
window.clearInterval(canvas.loadingInterval);
        
}
        
var ctx = canvas.getContext('2d');
        
if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
      
}
    
};
    
 
    
</
script
>
  
</
head
>
  
<
body
>
    
<
canvas
 
id="canvas" width="300" height="100" style="border:1px solid #69c">您的浏览器不支持html5哟</
canvas
>
    
<
p
>
    
<
input
 
type="button" οnclick="loadingObj.hide()" value="HideLoading"/>
    
<
input
 
type="button" οnclick="loadingObj.show()" value="showLoading"/>
    
</
p
>
    
<
script
>
    
var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
    
loadingObj.show();
    
</
script
>
  
</
body
>
</
html
>
目前从移动设备对Html5的支持来看,html5大有可为。

天下大势,合久必分,分久必和。PC开发时web应用在很大程度上统一了客户端程序;而现在移动开发使用不同的系统不同的语言,将来大多数应用必然会统一到一种语言。这种语言必然是html5 javascript.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供关于使用vue-infinite-loading实现php无限加载的一些指导。 首先,您需要安装vue-infinite-loading。可以通过npm或yarn安装,具体操作为: ``` npm install vue-infinite-loading --save ``` 或 ``` yarn add vue-infinite-loading ``` 安装完成后,在您的Vue组件中引入该组件: ```javascript import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, }; ``` 接下来,您需要在组件的template中使用该组件,并在data中定义需要加载的数据列表和当前页码: ```html <template> <div> <ul> <li v-for="item in list" :key="item.id">{{ item.title }}</li> </ul> <infinite-loading @infinite="infiniteHandler"></infinite-loading> </div> </template> <script> export default { data() { return { list: [], page: 1, }; }, methods: { infiniteHandler($state) { // 这里使用ajax请求获取数据,page为当前页码 axios.get('/api/list', { params: { page: this.page, }, }).then((res) => { if (res.data.length > 0) { this.list = this.list.concat(res.data); this.page += 1; $state.loaded(); } else { $state.complete(); } }); }, }, }; </script> ``` 在上面的代码中,我们使用axios发送ajax请求获取数据,并在请求成功后使用concat方法将新数据追加到原数据列表中。同时,我们使用$state.loaded()方法告诉vue-infinite-loading组件数据已经加载完毕,可以继续加载下一页数据。如果返回的数据为空,则使用$state.complete()方法告诉vue-infinite-loading组件数据已经全部加载完毕。 最后,您需要在样式中设置vue-infinite-loading组件的高度和样式: ```css .vue-infinite-loading { height: 60px; line-height: 60px; text-align: center; } ``` 这样就完成了使用vue-infinite-loading实现php无限加载的操作。希望以上内容能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值