原生JavaScript實(shí)現(xiàn)進(jìn)度條
JavaScript實(shí)現(xiàn)進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
進(jìn)度條實(shí)現(xiàn)介紹
使用JavaScript實(shí)現(xiàn)進(jìn)度條功能。
原理:通過(guò)鼠標(biāo)移動(dòng)事件,獲取鼠標(biāo)移動(dòng)的距離。
步驟:
(1)html 中 div 布局
(2)css 樣式編寫(xiě)
(3)JavaScript特效編寫(xiě)
html代碼
<body> <!-- 整體盒子 --> <div id="box"> <!-- 進(jìn)度條整體 --> <div id="progress"> <!-- 進(jìn)度條長(zhǎng)度 --> <div id="progress_head"></div> <!-- 進(jìn)度條移動(dòng)條 --> <span id="span"></span> <div> <!-- 顯示數(shù)據(jù) --> <div id="percentage">0%</div> </div> </body>
css樣式
<style>
/* 整體樣式,消除默認(rèn)樣式 */
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獲取節(jié)點(diǎn)
var oProgress=document.getElementById('progress');
var oProgress_head=document.getElementById('progress_head');
var oSpan=document.getElementById('span');
var oPercentage=document.getElementById('percentage')
//添加事件 鼠標(biāo)按下的事件
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>進(jìn)度條</title>
<style>
/* 整體樣式,消除默認(rèn)樣式 */
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">
<!-- 進(jìn)度條整體 -->
<div id="progress">
<!-- 進(jìn)度條長(zhǎng)度 -->
<div id="progress_head"></div>
<!-- 進(jìn)度條移動(dòng)條 -->
<span id="span"></span>
<div>
<!-- 顯示數(shù)據(jù) -->
<div id="percentage">0%</div>
</div>
</body>
</html>
<script>
//js獲取節(jié)點(diǎn)
var oProgress=document.getElementById('progress');
var oProgress_head=document.getElementById('progress_head');
var oSpan=document.getElementById('span');
var oPercentage=document.getElementById('percentage')
//添加事件 鼠標(biāo)按下的事件
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javascript 進(jìn)度條的幾種方法
- js實(shí)現(xiàn)進(jìn)度條的方法
- JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)加載進(jìn)度條代碼超簡(jiǎn)單
- js 進(jìn)度條實(shí)現(xiàn)代碼
- JS 進(jìn)度條效果實(shí)現(xiàn)代碼整理
- JS實(shí)現(xiàn)環(huán)形進(jìn)度條(從0到100%)效果
- Javascript jquery css 寫(xiě)的簡(jiǎn)單進(jìn)度條控件
- 用CSS+JS實(shí)現(xiàn)的進(jìn)度條效果效果
- js實(shí)現(xiàn)音頻控制進(jìn)度條功能
- PHP中使用Session配合Javascript實(shí)現(xiàn)文件上傳進(jìn)度條功能
相關(guān)文章
JavaScript字符串轉(zhuǎn)換數(shù)字的方法
這篇文章主要介紹了JavaScript字符串轉(zhuǎn)換數(shù)字的方法,文章圍繞JavaScript字符串轉(zhuǎn)換數(shù)字的相關(guān)資料展開(kāi)全文內(nèi)容,需要的小伙伴可以參考一下2021-12-12
基于JavaScript實(shí)現(xiàn)購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)購(gòu)物車(chē)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
javascript中l(wèi)ayim之查找好友查找群組
這篇文章主要介紹了javascript中l(wèi)ayim之查找好友查找群組,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
淺談JavaScript 執(zhí)行環(huán)境、作用域及垃圾回收
下面小編就為大家?guī)?lái)一篇淺談JavaScript:執(zhí)行環(huán)境、作用域及垃圾回收。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
JavaScript Canvas編寫(xiě)炫彩的網(wǎng)頁(yè)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了JavaScript Canvas編寫(xiě)炫彩的網(wǎng)頁(yè)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
JS基于HTML5的canvas標(biāo)簽實(shí)現(xiàn)炫目的色相球動(dòng)畫(huà)效果實(shí)例
這篇文章主要介紹了JS基于HTML5的canvas標(biāo)簽實(shí)現(xiàn)色相球效果,結(jié)合實(shí)例形式分析了基于canvas標(biāo)簽實(shí)現(xiàn)圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08

