原生JavaScript實現進度條

JavaScript實現進度條的具體代碼,供大傢參考,具體內容如下

進度條實現介紹

使用JavaScript實現進度條功能。

原理:通過鼠標移動事件,獲取鼠標移動的距離。

步驟:

(1)html 中 div 佈局
(2)css 樣式編寫
(3)JavaScript特效編寫

html代碼

<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樣式

<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代碼

<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>

效果圖

整體代碼

<!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>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: