JavaScript實(shí)現(xiàn)進(jìn)度條效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)進(jìn)度條效果的具體代碼,供大家參考,具體內(nèi)容如下
這次的效果圖如下:
這個(gè)案例做起來不難,在我練習(xí)的時(shí)候,新知識(shí)點(diǎn)是使用window.getComputedStyle()
函數(shù)獲取元素的寬度值
總的思路是在一個(gè)div盒子初始放入一個(gè)寬度為0的div盒子,然后在按鈕的onclick
回調(diào)函數(shù)中使用定時(shí)器改變其寬度值
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #container { width: 500px; height: 200px; margin: 50px auto; position: relative; } #box { width: 260px; height: 30px; border: 1px pink solid; border-radius: 16px; margin-bottom: 20px; padding: 1px; overflow: hidden; } #cont { width: 0; height: 100%; background-color: pink; border-radius: 16px; } #btn { position: absolute; margin-left: 110px; width: 50px; height: 30px; } #text { display: block; position: relative; left: 120px; margin-bottom: 20px; } </style> </head> <body> <div id="container"> <div id="box" data-content-before="22"> <div id="cont"></div> </div> <div id="text">0%</div> <button id="btn">提交</button> </div> <script> let box = document.getElementById("box"); let btn = document.getElementById("btn"); let cont = document.getElementById("cont"); let text = document.getElementById("text"); function getstyle(obj, name) { if (window.getComputedStyle) { return window.getComputedStyle(obj, null)[name]; } else { return obj.currentStyle[name]; } } btn.onclick = function () { let ini = 0; let num = setInterval(() => { let tem = parseInt(window.getComputedStyle(cont, null)["width"]); let now = tem + 26; if (tem >= 260) { console.log(now); clearInterval(num); return; } cont.style.width = now + "px"; ini = ini + 10; text.innerText = ini + "%"; }, 80); } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javascript 進(jìn)度條的幾種方法
- js實(shí)現(xiàn)進(jìn)度條的方法
- JavaScript實(shí)現(xiàn)網(wǎng)頁加載進(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 寫的簡(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引用類型Object常見用法實(shí)例分析
這篇文章主要介紹了JavaScript引用類型Object常見用法,簡(jiǎn)單描述了javascript基本數(shù)據(jù)類型,并結(jié)合實(shí)例形式分析了引用類型Object基本創(chuàng)建、賦值、訪問屬性等基本操作技巧,需要的朋友可以參考下2018-08-08使用JavaScript動(dòng)態(tài)設(shè)置樣式實(shí)現(xiàn)代碼(2)
使用onmouseover和onmouseout事件實(shí)現(xiàn)不同的效果而且是使用js動(dòng)態(tài)實(shí)現(xiàn),本文有利于鞏固你js與css方面的知識(shí),感興趣的你可以了解下哦,希望本文對(duì)你有所幫助2013-01-01javascript 循環(huán)調(diào)用示例介紹
循環(huán)調(diào)用,如果已經(jīng)獲取到了結(jié)果,則退出循環(huán),下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以嘗試操作下2013-11-11利用BootStrap的Carousel.js實(shí)現(xiàn)輪播圖動(dòng)畫效果
這篇文章主要介紹了利用BootStrap的Carousel.js實(shí)現(xiàn)輪播圖動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下2016-12-12javascript 動(dòng)態(tài)創(chuàng)建表格
這篇文章主要介紹了javascript 動(dòng)態(tài)創(chuàng)建表格,需要的朋友可以參考下2015-01-01