在開發網頁或應用程式時,步驟條是一個非常重要的元素,它可以幫助使用者清楚地了解當前進度,以及還有多少工作要完成。通常,一個步驟條應該顯示使用者已經完成的步驟數以及未完成的步驟數,並且需要精確地顯示當前進度百分比。在本文中,我們將介紹如何使用JavaScript顯示一個帶有百分比的步驟條。
步驟一:HTML結構
首先,我們需要在HTML中建立一個步驟條的骨架。我們可以使用一個簡單的無序號表(ul)和一些有序號的清單項目(li),如下所示:
- 1 步骤一
- 2 步骤二
- 3 步骤三
- 4 步骤四
我們可以看到,每個清單項目都包含一個步驟號(由<span class="step"></span>
元素表示)和一個標題(由<span class="title"></span>
元素表示)。在第一個清單項目中,我們也使用了active
類別來表示目前步驟。
步驟二:CSS樣式
接下來,我們需要在CSS中為步驟條定義樣式。我們將使用flexbox來對齊和佈局清單項,如下所示:
.progress { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; list-style: none; } .progress li { flex: 1; text-align: center; position: relative; z-index: 1; } .progress li.active::before { content: ''; position: absolute; top: 50%; left: -50%; width: 200%; height: 4px; background-color: #4caf50; transform: translateY(-50%); z-index: -1; } .progress .step { display: block; width: 30px; height: 30px; line-height: 30px; border-radius: 50%; background-color: #f2f2f2; color: #999; margin-bottom: 10px; } .progress li.active .step { background-color: #4caf50; color: #fff; } .progress .title { display: block; font-size: 12px; color: #777; text-transform: uppercase; margin-bottom: 5px; } .progress li:first-child .title { text-align: left; } .progress li:last-child .title { text-align: right; }
在這個CSS樣式中,我們使用了flexbox來將每個清單項目垂直對齊,並使用justify-content: space-between
讓每個清單項目之間留有一些空間。我們也使用偽元素::before
和position: absolute
將目前步驟背景顏色和百分比進度條呈現出來。注意,我們還為每個清單項目中的步驟號碼和標題添加了相應的樣式。
步驟三:JavaScript程式碼
最後,我們需要寫一些JavaScript程式碼來計算進度百分比,並在步驟列中顯示它。對於每個清單項,我們分別計算已完成的步驟數和總步驟數,然後將進度的百分比顯示在步驟編號旁的元素中。我們可以使用以下程式碼來實現這一點:
// 获取步骤条的UL元素 const progress = document.querySelector('.progress'); // 获取步骤条中的所有列表项 const steps = progress.querySelectorAll('li'); // 遍历每个列表项 steps.forEach((step, index) => { // 获取当前列表项的步骤号 const stepNumber = step.querySelector('.step'); // 计算已完成的步骤数和总步骤数 const completed = index; const total = steps.length - 1; // 计算进度的百分比 const percent = Math.round((completed / total) * 100); // 在步骤号旁边的元素中显示百分比 stepNumber.innerHTML = `${percent}%`; });
注意,我們在這段程式碼中使用了Math.round()
函數來將計算出的進度百分比四捨五入到整數,從而避免小數位誤差所導致的顯示問題。
最後,我們可以將上述這些程式碼儲存到我們的HTML檔案中,並開啟該檔案以查看步驟條及其百分比進度的效果。是不是很簡單?透過上述這些步驟,我們就可以輕鬆地建立一個帶有百分比進度的步驟條,並使使用者清晰地了解他們當前的進度。
以上是javascript步驟條顯示百分比怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Include:1)AsteeplearningCurvedUetoItsVasteCosystem,2)SeochallengesWithClient-SiderEndering,3)潛在的PersperformanceissuesInsuesInlArgeApplications,4)ComplexStateStateManagementAsappsgrow和5)TheneedtokeEedtokeEedtokeEppwithitsrapideDrapidevoltolution.thereedtokeEppectortorservolution.thereedthersrapidevolution.ththesefactorsshesssheou

reactischallengingforbeginnersduetoitssteplearningcurveandparadigmshifttocoment oparchitecent.1)startwithofficialdocumentationforasolidFoundation.2)了解jsxandhowtoembedjavascriptwithinit.3)

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

javascriptfatigueinrectismanagbaiblewithstrategiesLike just just in-timelearninganning and CuratedInformationsources.1)學習whatyouneedwhenyouneedit

tateractComponents通過theusestatehook,使用jestandReaCtTestingLibraryToSigulationsimintionsandIntractions and verifyStateChangesInTheUI.1)underthecomponentAndComponentAndComponentAndConconentAndCheckInitialState.2)模擬useruseruserusertactionslikeclicksorformsorformsormissions.3)

KeysinreactarecrucialforopTimizingPerformanceByingIneFefitedListupDates.1)useKeyStoIndentifyAndTrackListelements.2)避免使用ArrayIndicesasKeystopreventperformansissues.3)ChooSestableIdentifierslikeIdentifierSlikeItem.idtomaintainAinainCommaintOnconMaintOmentStateAteanDimpperperFermerfermperfermerformperfermerformfermerformfermerformfermerment.ChosestopReventPerformissues.3)

ReactKeySareUniqueIdentifiers usedwhenrenderingListstoimprovereConciliation效率。 1)heelPreactrackChangesInListItems,2)使用StableanDuniqueIdentifiersLikeItifiersLikeItemidSisRecumended,3)避免使用ArrayIndicesaskeyindicesaskeystopreventopReventOpReventSissUseSuseSuseWithReRefers和4)

獨特的keysarecrucialinreactforoptimizingRendering和MaintainingComponentStateTegrity.1)useanaturalAlaluniqueIdentifierFromyourDataiFabable.2)ifnonaturalalientedifierexistsistsists,generateauniqueKeyniqueKeyKeyLiquekeyperaliqeyAliqueLiqueAlighatiSaliqueLiberaryLlikikeuuId.3)deversearrayIndiceSaskeyseSecialIndiceSeasseAsialIndiceAseAsialIndiceAsiall


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

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

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

Dreamweaver CS6
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。