JavaScript实现进度条的具体代码,供大家参考,具体内容如下
进度条实现介绍
使用JavaScript实现进度条功能。
原理:通过鼠标移动事件,获取鼠标移动的距离。
步骤:
(1)html 中 div 布局
(2)css 样式编写
(3)JavaScript特效编写
html代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< body >
<!-- 整体盒子 -->
< div id = "box" >
<!-- 进度条整体 -->
< div id = "progress" >
<!-- 进度条长度 -->
< div id = "progress_head" ></ div >
<!-- 进度条移动条 -->
< span id = "span" ></ span >
< div >
<!-- 显示数据 -->
< div id = "percentage" >0%</ div >
</ div >
</ body >
|
css样式
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
|
<style>
/* 整体样式,消除默认样式 */
body{
margin : 0 ;
padding : 0 ;
}
#box{
position : relative ;
width : 1000px ;
height : 30px ;
margin : 100px auto ;
}
#progress{
position : relative ;
width : 900px ;
height : 30px ;
background : #999999 ;
border-radius: 8px ;
margin : 0 auto ;
}
#progress_head{
width : 0px ;
height : 100% ;
border-top-left-radius: 8px ;
border-bottom-left-radius: 8px ;
background : #9933CC ;
}
span{
position : absolute ;
width : 20px ;
height : 38px ;
background : #9933CC ;
top : -4px ;
left : 0px ;
cursor : pointer ;
}
#percentage{
position : absolute ;
line-height : 30px ;
text-align : center ;
right : -44px ;
top : 0 ;
}
</style>
|
JavaScript代码
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
|
<script>
//js获取节点
var oProgress=document.getElementById( 'progress' );
var oProgress_head=document.getElementById( 'progress_head' );
var oSpan=document.getElementById( 'span' );
var oPercentage=document.getElementById( 'percentage' )
//添加事件 鼠标按下的事件
oSpan.onmousedown= function (event){
var event=event || window.event;
var x=event.clientX-oSpan.offsetLeft;
document.onmousemove= function (){
var event=event || window.event;
var wX=event.clientX-x;
if (wX<0)
{
wX=0;
} else if (wX>=oProgress.offsetWidth-20)
{
wX=oProgress.offsetWidth - 20;
}
oProgress_head.style.width=wX+ 'px' ;
oSpan.style.left=wX+ 'px' ;
oPercentage.innerHTML=parseInt(wX/(oProgress.offsetWidth-20)*100)+ '%' ;
return false ;
};
document.onmouseup= function (){
document.onmousemove= null ;
};
};
</script>
|
效果图
整体代码
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<!DOCTYPE>
< html lang = "en" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >进度条</ title >
< style >
/* 整体样式,消除默认样式 */
body{
margin:0;
padding:0;
}
#box{
position:relative;
width:1000px;
height:30px;
margin:100px auto;
}
#progress{
position:relative;
width:900px;
height:30px;
background:#999999;
border-radius:8px;
margin:0 auto;
}
#progress_head{
width:0px;
height:100%;
border-top-left-radius:8px;
border-bottom-left-radius:8px;
background:#9933CC;
}
span{
position:absolute;
width:20px;
height:38px;
background:#9933CC;
top:-4px;
left:0px;
cursor:pointer;
}
#percentage{
position:absolute;
line-height:30px;
text-align:center;
right:-44px;
top:0;
}
</ style >
</ head >
< body >
<!-- 整体盒子 -->
< div id = "box" >
<!-- 进度条整体 -->
< div id = "progress" >
<!-- 进度条长度 -->
< div id = "progress_head" ></ div >
<!-- 进度条移动条 -->
< span id = "span" ></ span >
< div >
<!-- 显示数据 -->
< div id = "percentage" >0%</ div >
</ div >
</ body >
</ html >
< script >
//js获取节点
var oProgress=document.getElementById('progress');
var oProgress_head=document.getElementById('progress_head');
var oSpan=document.getElementById('span');
var oPercentage=document.getElementById('percentage')
//添加事件 鼠标按下的事件
oSpan.onmousedown=function(event){
var event=event || window.event;
var x=event.clientX-oSpan.offsetLeft;
document.onmousemove=function(){
var event=event || window.event;
var wX=event.clientX-x;
if(wX< 0 )
{
wX = 0 ;
}else if(wX>=oProgress.offsetWidth-20)
{
wX=oProgress.offsetWidth - 20;
}
oProgress_head.style.width=wX+'px';
oSpan.style.left=wX+'px';
oPercentage.innerHTML=parseInt(wX/(oProgress.offsetWidth-20)*100)+'%';
return false;
};
document.onmouseup=function(){
document.onmousemove=null;
};
};
</ script >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44134972/article/details/85332929