js實(shí)現(xiàn)滑動(dòng)進(jìn)度條效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)滑動(dòng)進(jìn)度條效果的具體代碼,供大家參考,具體內(nèi)容如下
進(jìn)度條:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>js滑動(dòng)進(jìn)度條效果</title> <style> *{margin:0;padding:0;user-select:none;} .progress-bar{position:relative;height:10px;width:400px;margin:200px auto;background:#ebeef5;border-radius:10px;} .progress-bar .pro-bar{position:absolute;left:0;height:10px;width:10px;background:#409eff;} .progress-bar .min-num{position:absolute;left:-20px;top:-5px;} .progress-bar .max-num{position:absolute;right:-40px;top:-5px;} .progress-bar .block {position:absolute;height:30px;width:10px;background:#ccc;top:-10px;border-radius:10px;} .progress-bar .block div{position:absolute;display:none;left:-20px;top:-45px;width:50px;height:30px;text-align:center;line-height:30px;background:#ccc;border-radius:20px;} .progress-bar .block:hover div{display:block;font-size:10%;color:#fff;background:#409eff;} </style> </head> <body> <div class="progress-bar"> <span class="min-num">0</span> <span class="max-num">100</span> <div class="pro-bar"></div> <div class="block"> <div>0</div> </div> </div> </body> <script> (function(){ let moveBlock = document.querySelector('.block'); let proBar = document.querySelector('.pro-bar'); let flag = false; let startX = null; let moveMax = (400 - 10); // 背景條寬度減去滑塊的寬度 moveBlock.onmousedown = function(e){ startX = e.pageX; moveBlock.style.left ? moveBlock.style.left : moveBlock.style.left = '0px'; let startLeft = parseInt(moveBlock.style.left); document.onmousemove = function(e){ let moveX = (e.pageX - startX) > 0 ? true : false; let moveSection = startLeft + (e.pageX - startX); // 限定移動(dòng)范圍 if (moveSection >= 0 && moveSection <= moveMax) { let percent = ((startLeft + (e.pageX - startX)) / moveMax).toFixed(4) * 100; percent.toString().length > 5 ? percent = percent.toString().subStr(0, 5) : percent = percent.toString(); moveBlock.style.left = startLeft + (e.pageX - startX) + 'px'; proBar.style.width = moveBlock.style.left; moveBlock.querySelector('div').innerText = percent + '%'; } }; }; // 鼠標(biāo)松開(kāi)移除事件 moveBlock.onmouseup = function(){ document.onmousemove = null; }; })(); </script> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS 兩日期相減,獲得天數(shù)的小例子(兼容IE,FF)
這篇文章介紹了兩日期相減,獲得天數(shù)的小例子,有需要的朋友可以參考一下2013-07-07js中如何復(fù)制一個(gè)對(duì)象并獲取其所有屬性和屬性對(duì)應(yīng)的值
如果知道這個(gè)對(duì)象的所有屬性自然就可以重新new一個(gè),然后對(duì)每個(gè)屬性賦值,就可以做到,但如果不知道呢?如何創(chuàng)建一個(gè)內(nèi)容相同 的對(duì)象呢?下面有個(gè)不錯(cuò)的示例,大家可以看看2013-10-10JavaScript常見(jiàn)繼承模式實(shí)例小結(jié)
這篇文章主要介紹了JavaScript常見(jiàn)繼承模式,結(jié)合實(shí)例形式總結(jié)分析了javascript原型鏈繼承、構(gòu)造函數(shù)繼承、組合繼承、原型式繼承、寄生式繼承等相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-01-01Bootstrap基本組件學(xué)習(xí)筆記之input輸入框組(9)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之input輸入框組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12webpack與SPA實(shí)踐之管理CSS等資源的方法
本篇文章主要介紹了webpack與SPA實(shí)踐之管理CSS等資源的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12使用JavaScriptCore實(shí)現(xiàn)OC和JS交互詳解
JavaScriptCore是webkit的一個(gè)重要組成部分,主要是對(duì)JS進(jìn)行解析和提供執(zhí)行環(huán)境。下面這篇文章主要給大家介紹了使用JavaScriptCore實(shí)現(xiàn)OC和JS交互的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-03-03基于javascript實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03