原生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í)有所幫助,也希望大家多多支持腳本之家。
- javascript 進(jìn)度條的幾種方法
- js實(shí)現(xiàn)進(jìn)度條的方法
- JavaScript實(shí)現(xiàn)網(wǎng)頁加載進(jì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 寫的簡單進(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實(shí)現(xiàn)俄羅斯方塊游戲的思路和方法
至于俄羅斯方塊的話,有很多的難點(diǎn),如果有JS去寫的話,要考慮到碰撞啊,邊界啊,下落等問題,本文這些問題大部分都有考慮到,這里給大家提供一部分思路,拋磚引玉,有需要的小伙伴可以參考下。2015-04-04淺談如何使用webpack構(gòu)建多頁面應(yīng)用
這篇文章主要介紹了淺談如何使用webpack構(gòu)建多頁面應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Javascript 強(qiáng)制類型轉(zhuǎn)換函數(shù)
javascript是弱類型的語言,所以強(qiáng)制類型轉(zhuǎn)換還是比較重要的,下面看一下它的幾個(gè)強(qiáng)制轉(zhuǎn)換的函數(shù)2009-05-05細(xì)數(shù)promise與async/await的使用及區(qū)別說明
這篇文章主要介紹了細(xì)數(shù)promise與async/await的使用及區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07