JavaScript實作進度條的幾種方法介紹
我們在之前的文章為大家介紹了關於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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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/imghw/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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

而後悔莫及、人們常常會因為一些原因不小心刪除某些聯絡人、微信作為一款廣泛使用的社群軟體。幫助用戶解決這個問題,本文將介紹如何透過簡單的方法找回被刪除的聯絡人。 1.了解微信聯絡人刪除機制這為我們找回被刪除的聯絡人提供了可能性、微信中的聯絡人刪除機制是將其從通訊錄中移除,但並未完全刪除。 2.使用微信內建「通訊錄恢復」功能微信提供了「通訊錄恢復」節省時間和精力,使用者可以透過此功能快速找回先前刪除的聯絡人,功能。 3.進入微信設定頁面點選右下角,開啟微信應用程式「我」再點選右上角設定圖示、進入設定頁面,,

七彩虹主機板在中國國內市場享有較高的知名度和市場佔有率,但是有些七彩虹主機板的用戶還不清楚怎麼進入bios進行設定呢?針對這一情況,小編專門為大家帶來了兩種進入七彩虹主機板bios的方法,快來試試吧!方法一:使用u盤啟動快捷鍵直接進入u盤裝系統七彩虹主機板一鍵啟動u盤的快捷鍵是ESC或F11,首先使用黑鯊裝機大師製作一個黑鯊U盤啟動盤,然後開啟電腦,當看到開機畫面的時候,連續按下鍵盤上的ESC或F11鍵以後將會進入到一個啟動項順序選擇的窗口,將遊標移到顯示“USB”的地方,然

番茄小說是一款非常熱門的小說閱讀軟體,我們在番茄小說中經常會有新的小說和漫畫可以去閱讀,每一本小說和漫畫都很有意思,很多小伙伴也想著要去寫小說來賺取賺取零用錢,在把自己想要寫的小說內容編輯成文字,那麼我們要怎麼樣在這裡面去寫小說呢?小伙伴們都不知道,那就讓我們一起到本站本站中花點時間來看寫小說的方法介紹。分享番茄小說寫小說方法教學 1、先在手機上打開番茄免費小說app,點擊個人中心——作家中心 2、跳到番茄作家助手頁面——點擊創建新書在小說的結

手機遊戲成為了人們生活中不可或缺的一部分,隨著科技的發展。它以其可愛的龍蛋形象和有趣的孵化過程吸引了眾多玩家的關注,而其中一款備受矚目的遊戲就是手機版龍蛋。幫助玩家們在遊戲中更好地培養和成長自己的小龍,本文將向大家介紹手機版龍蛋的孵化方法。 1.選擇合適的龍蛋種類玩家需要仔細選擇自己喜歡並且適合自己的龍蛋種類,根據遊戲中提供的不同種類的龍蛋屬性和能力。 2.提升孵化機的等級玩家需要透過完成任務和收集道具來提升孵化機的等級,孵化機的等級決定了孵化速度和孵化成功率。 3.收集孵化所需的資源玩家需要在遊戲中

人臉偵測辨識技術已經是一個比較成熟且應用廣泛的技術。而目前最廣泛的網路應用語言非JS莫屬,在Web前端實現人臉偵測辨識相比後端的人臉辨識有優勢也有弱勢。優點包括減少網路互動、即時識別,大大縮短了使用者等待時間,提高了使用者體驗;弱勢是:受到模型大小限制,其中準確率也有限。如何在web端使用js實現人臉偵測呢?為了實現Web端人臉識別,需要熟悉相關的程式語言和技術,如JavaScript、HTML、CSS、WebRTC等。同時也需要掌握相關的電腦視覺和人工智慧技術。值得注意的是,由於Web端的計

Win11管理員權限取得方法匯總在Windows11作業系統中,管理員權限是非常重要的權限之一,可以讓使用者對系統進行各種操作。有時候,我們可能需要取得管理員權限來完成一些操作,例如安裝軟體、修改系統設定等。下面就為大家總結了一些取得Win11管理員權限的方法,希望能幫助大家。 1.使用快捷鍵在Windows11系統中,可以透過快捷鍵的方式快速開啟命令提

Oracle版本查詢方法詳解Oracle是目前世界上最受歡迎的關聯式資料庫管理系統之一,它提供了豐富的功能和強大的效能,廣泛應用於企業。在進行資料庫管理和開發過程中,了解Oracle資料庫的版本是非常重要的。本文將詳細介紹如何查詢Oracle資料庫的版本信息,並給出具體的程式碼範例。查詢資料庫版本的SQL語句在Oracle資料庫中,可以透過執行簡單的SQL語句

字體大小的設定成為了重要的個人化需求,隨著手機成為人們日常生活的重要工具。以滿足不同使用者的需求、本文將介紹如何透過簡單的操作,提升手機使用體驗,調整手機字體大小。為什麼需要調整手機字體大小-調整字體大小可以使文字更清晰易讀-適合不同年齡段用戶的閱讀需求-方便視力不佳的用戶使用手機系統自帶字體大小設置功能-如何進入系統設置界面-在在設定介面中找到並進入"顯示"選項-找到"字體大小"選項並進行調整第三方應用調整字體大小-下載並安裝支援字體大小調整的應用程式-開啟應用程式並進入相關設定介面-根據個人
