原生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)度條功能。
原理:通過鼠標(biāo)移動(dòng)事件,獲取鼠標(biāo)移動(dòng)的距離。
步驟:
(1)html 中 div 布局(2)css 樣式編寫(3)JavaScript特效編寫
html代碼<body> <!-- 整體盒子 --> <div id='box'> <!-- 進(jìn)度條整體 --> <div id='progress'> <!-- 進(jìn)度條長度 --> <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)度條長度 --> <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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫方法2. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼3. XML入門的常見問題(二)4. layui Ajax請(qǐng)求給下拉框賦值的實(shí)例5. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐6. ASP.NET MVC使用異步Action的方法7. asp知識(shí)整理筆記4(問答模式)8. CSS hack用法案例詳解9. 詳解瀏覽器的緩存機(jī)制10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
