我們在之前的文章為大家介紹了關於JavaScript實作進度條的實例,我們都知道在實際專案中,進度條的用處還是非常大的,今天我們就大家繼續介紹下JavaScript實現進度條的有哪幾種方式!
我們先看看最終效果:
第一步,基本上
#建立基本的程式碼,看效果演示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>制作进度条的两种方法</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style> *{ margin:0; padding:0; } body{font-size:12px;}h1{font-size:15px;} .wrap{ font-size: 12px; margin:40px auto; width:400px; } /*进度条样式*/ .progressbar_1{ background-color:#eee; height:16px; width:150px; border:1px solid #bbb; color:#222; } .progressbar_1 .bar { background-color:#6CAF00; height:16px; width:0; } </style> </head> <body> <div class="wrap"> <h1 id="构建基本的HTML结构和CSS样式">1,构建基本的HTML结构和CSS样式</h1> 0%的状态: <div class="progressbar_1"> <div class="bar"></div> </div> 50%的状态: <div class="progressbar_1"> <div class="bar" style="width: 50%;"></div> </div> 80%的状态: <div class="progressbar_1"> <div class="bar" style="width: 80%;"></div> </div> 100%的状态: <div class="progressbar_1"> <div class="bar" style="width: 100%;"></div> </div> 这步非常简单,相信大家都能看明白。 </div> </body> </html>
第二步,增加進度條文字顯示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>制作进度条的两种方法</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style> *{ margin:0; padding:0; } body{font-size:12px;}h1{font-size:15px;} .wrap{ font-size: 12px; margin:40px auto; width:400px; } /*进度条样式*/ .progressbar_1{ background-color:#eee; color:#222; height:16px; width:150px; border:1px solid #bbb; } .progressbar_1 .bar { background-color:#6CAF00; height:16px; width:0; } /*绝对定位*/ .progressbar_2{ background-color:#eee; color:#222; height:16px; width:150px; border:1px solid #bbb; text-align:center; position:relative; } .progressbar_2 .bar { background-color:#6CAF00; height:16px; width:0; position:absolute; left:0; top:0; } .progressbar_2 .text { height:16px; position:absolute; left:0; top:0; width:100%; line-height:16px; } /*绝对定位 + z-index */ .progressbar_3{ background-color:#eee; color:#222; height:16px; width:150px; border:1px solid #bbb; text-align:center; position:relative; } .progressbar_3 .bar { background-color:#6CAF00; height:16px; width:0; position:absolute; left:0; top:0; z-index:10; } .progressbar_3 .text { height:16px; position:absolute; left:0; top:0; width:100%; line-height:16px; z-index:100; } </style> </head> <body> <div class="wrap"> <h1 id="给进度条增加文字显示">2,给进度条增加文字显示</h1> 一个进度条需要包含对应的进度文字显示,本例中,我希望文字显示在进度条的中间。 接下来,我们在进度条元素内增加一个显示 文字的div。 很显然,直接添加div后,div会占据原先绿色进度条的位置。看下面例子: <div class="progressbar_1"> <div >50%</div> <div class="bar" style="width: 50%;"></div> </div> 那么有什么办法使得文字 和 绿色进度条 同时显示在 一起呢? 估计你也想到了,那就是绝对定位。通过对父元素添加相对定位,然后给子元素添加绝对定位,设置子元素的左边距和上边距都为0。 看下面的例子: <div class="progressbar_2"> <div class="text">50%</div> <div class="bar" style="width: 50%;"></div> </div> 这时你会发现文字被遮盖了,相信你第一时间就会想到用 z-index 去设置他们的层级关系。 对,我也选择的是它。通过给他们设置不同的z-index值来确定层级关系,值越大越显示在上面。看下面例子: <div class="progressbar_3"> <div class="text">50%</div> <div class="bar" style="width: 50%;"></div> </div> <div class="progressbar_3"> <div class="text">80%</div> <div class="bar" style="width: 80%;"></div> </div> <div class="progressbar_3"> <div class="text">100%</div> <div class="bar" style="width: 100%;"></div> </div> 这是第一种方式,也是目前最为流行的 制作进度条的方式。 他的原理就是采用 相对定位和绝对定位,然后通过背景不同来控制进度。 接下来我们看第二种方式制作进度条。 </div> </body> </html>
他的原理就是採用相對定位和絕對定位,然後再透過背景不同來控制進度。 接下來我們來看第二種方式製作進度條。
第三步,製作進度條的第二種方式---直接使用圖片+背景圖片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style> *{ margin:0; padding:0; } body{font-size:12px;}h1{font-size:15px;} .wrap{ font-size: 12px; margin:40px auto; width:400px; } /*不带文字*/ .progress{ border:0; width: 152px; height: 18px; background:url(img/progress.gif) no-repeat -100px 50%; } /*带文字显示*/ .progressbar_3{ width:152px; position:relative; height:18px; text-align:center; position:relative; color:#222; } .progressbar_3 .text{ width:152px; position:absolute; left:0; top:0; height:18px; line-height:18px; z-index:100; } .progressbar_3 .progress{ width:152px; position:absolute; left:0; top:0; height:18px; z-index:10; } </style> </head> <body> <div class="wrap"> <h1 id="制作进度条的第二种方式-直接使用图片-背景图片">3,制作进度条的第二种方式---直接使用图片+背景图片</h1> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="0%" alt="0%" style="background-position:-152px 50%;"/> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="20%" alt="20%" style="background-position:-120px 50%;"/> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="50%" alt="50%" style="background-position:-76px 50%;"/> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="80%" alt="80%" style="background-position:-30px 50%;"/> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="100%" alt="100%" style="background-position: 0 50%;"/> 如果你想在上面进度条上添加进度显示的文字,你应该知道怎么办了(如果你仔细阅读过前面的例子的话)。 对,就是使用绝对定位+相对定位来控制。 <div class="progressbar_3"> <div class="text">0%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="0%" alt="0%" style="background-position:-152px 50%;"/> </div> <div class="progressbar_3"> <div class="text">20%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="20%" alt="20%" style="background-position:-120px 50%;"/> </div> <div class="progressbar_3"> <div class="text">50%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="50%" alt="50%" style="background-position:-76px 50%;"/> </div> <div class="progressbar_3"> <div class="text">80%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="80%" alt="80%" style="background-position:-30px 50%;"/> </div> <div class="progressbar_3"> <div class="text">100%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="100%" alt="100%" style="background-position: 0 50%;"/> </div> 这种方式的原理就是 利用 一张img图片,然后给img图片又设置背景图,通过背景图的background position 属性 来显示不同的部分。 当然这种方式 对图片有点要求 。你仔细一点就会发现,宽度是img的2倍。 另外图片必须是gif透明的,也就是说空心的。呵呵。看不到进度条。 总的来说,这应该算一个技巧,在实际项目中,也是能使用的。 <p style="float:right;">Dev By <a href="http://www.jb51.net" >jb51.net</a></p> </div> </body> </html>
第四步,應用
最後我們來看一個簡單的應用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style> *{ margin:0; padding:0; } body{font-size:12px;}h1{font-size:15px;} .wrap{ font-size: 12px; margin:40px auto; width:400px; } /*不带文字*/ .progress{ border:0; width: 152px; height: 18px; background:url(img/progress.gif) no-repeat -100px 50%; } /*带文字显示*/ .progressbar_3{ width:152px; position:relative; height:18px; text-align:center; position:relative; color:#222; } .progressbar_3 .text{ width:152px; position:absolute; left:0; top:0; height:18px; line-height:18px; z-index:100; } .progressbar_3 .progress{ width:152px; position:absolute; left:0; top:0; height:18px; z-index:10; } </style> <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript" language="javascript" charset="gb2312"></script> <script> $(function(){ var $txt = $(".progressbar_3 .text"); var $progress = $(".progressbar_3 .progress"); $("#addTo").click(function(){ $progress.animate({"backgroundPosition":"-76px 50%"},500,function(){ $(this).attr({"alt":"50%","title":"50%"}); $txt.text("50%"); $("<p>进度条加载完毕!</p>").appendTo(".wrap"); }); }); $("#cutdown").click(function(){ $progress.animate({"backgroundPosition":"-120px 50%"},500,function(){ $(this).attr({"alt":"20%","title":"20%"}); $txt.text("20%"); $("<p>进度条加载完毕!</p>").appendTo(".wrap"); }); }); }); </script> </head> <body> <div class="wrap"> <h1 id="应用">4,应用</h1> <div class="progressbar_3"> <div class="text">0%</div> <img class="progress lazy" src="/static/imghwm/default1.png" data-src="img/bg.gif" style="max-width:90%" style="max-width:90%" title="0%" alt="0%" style="background-position:-152px 50%;"/> </div> <button id="addTo" >增加至50%</button> <button id="cutdown" >减少至20%</button> 我使用的是第二种方式来做控制,通过animate来控制图片的background-position来达到进度条效果。 当然你也可以使用第一种方式做,第一种方式需要改变的是进度条的 width 属性。 <p style="float:right;">Dev By <a href="http://www.jb51.net" >jb51.net</a></p> </div> </body> </html>
#透過animate來控制圖片的background-position來達到進度條效果。
當然你也可以用第一種方式做,第一種方式需要改變的是進度條的 width 屬性。
總結:
透過本文的學習相信小夥伴們對JavaScript實現進度條的方法有了進一步的了解,每個方法都有不同,小夥伴可以根據自己的需求選擇合適的方法!
相關推薦:
以上是JavaScript實作進度條的幾種方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver CS6
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中