原生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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)2. CSS自定義滾動(dòng)條樣式案例詳解3. 關(guān)于Mysql-connector-java驅(qū)動(dòng)版本問題總結(jié)4. python 批量下載bilibili視頻的gui程序5. python numpy庫np.percentile用法說明6. Ajax提交post請求案例分析7. Android Studio 3.6 正式版終于發(fā)布了,快來圍觀8. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)之類屬性與類常量實(shí)現(xiàn)方法分析
