html中制作進(jìn)度環(huán)的核心答案是利用svg或css繪制圓形并通過(guò)控制stroke屬性實(shí)現(xiàn)動(dòng)態(tài)效果。1. svg方式通過(guò)
簡(jiǎn)單來(lái)說(shuō),HTML中制作進(jìn)度環(huán),核心在于利用SVG或CSS,巧妙地繪制圓形,并通過(guò)控制stroke(描邊)的屬性,動(dòng)態(tài)展示進(jìn)度。SVG更靈活,CSS則更簡(jiǎn)潔。
解決方案
制作進(jìn)度環(huán)主要有兩種方式:SVG 和 CSS。各有優(yōu)劣,選擇哪種取決于你的具體需求。
1. SVG 方式
立即學(xué)習(xí)“前端免費(fèi)學(xué)習(xí)筆記(深入)”;
SVG(Scalable Vector Graphics)提供更強(qiáng)大的圖形控制能力。
-
基本原理: 使用
元素繪制圓形,利用 stroke-dasharray 和 stroke-dashoffset 屬性來(lái)控制描邊的顯示。stroke-dasharray 定義描邊的虛線模式,stroke-dashoffset 定義虛線模式的偏移量。通過(guò)改變 stroke-dashoffset,可以實(shí)現(xiàn)進(jìn)度條的動(dòng)態(tài)效果。 -
示例代碼:
<svg width="200" height="200"> <circle cx="100" cy="100" r="80" stroke="#ccc" stroke-width="10" fill="none"/> <circle cx="100" cy="100" r="80" stroke="#4CAF50" stroke-width="10" fill="none" stroke-dasharray="502.65" stroke-dashoffset="502.65" transform="rotate(-90 100 100)" style="transition: stroke-dashoffset 0.3s linear;"/> </svg> <script> // 模擬進(jìn)度更新 setTimeout(() => { const circle = document.querySelector('svg circle:nth-child(2)'); circle.style.strokeDashoffset = 251.33; // 50% }, 1000); </script>
- cx 和 cy 定義圓心坐標(biāo)。
- r 定義半徑。
- stroke 定義描邊顏色。
- stroke-width 定義描邊寬度。
- fill=”none” 表示不填充。
- stroke-dasharray 的值等于圓的周長(zhǎng) (2 PI r),這里是 2 3.14159 80 ≈ 502.65。
- stroke-dashoffset 初始值設(shè)為 stroke-dasharray 的值,表示進(jìn)度為 0%。
- transform=”rotate(-90 100 100)” 將圓旋轉(zhuǎn) -90 度,使進(jìn)度從頂部開始。
- transition 屬性添加過(guò)渡效果。
-
優(yōu)點(diǎn): 靈活,可定制性強(qiáng),可以添加復(fù)雜的動(dòng)畫效果。
-
缺點(diǎn): 代碼相對(duì)復(fù)雜,需要理解 SVG 的屬性。
2. CSS 方式
CSS 方式更簡(jiǎn)潔,但靈活性稍差。
-
基本原理: 使用 border-radius 創(chuàng)建圓形,通過(guò) clip 屬性裁剪圓形,再利用 transform: rotate() 旋轉(zhuǎn)元素,實(shí)現(xiàn)進(jìn)度條的效果。
-
示例代碼:
<div class="circle-progress"> <div class="circle-progress-mask"> <div class="circle-progress-fill"></div> </div> <div class="circle-progress-mask"> <div class="circle-progress-fill"></div> </div> </div> <style> .circle-progress { width: 100px; height: 100px; border-radius: 50%; background-color: #ccc; position: relative; } .circle-progress-mask { width: 50%; height: 100%; position: absolute; overflow: hidden; } .circle-progress-mask:nth-child(1) { clip: rect(0, 50px, 100px, 0); } .circle-progress-mask:nth-child(2) { left: 50%; clip: rect(0, 100px, 100px, 50px); } .circle-progress-fill { width: 100px; height: 100px; border-radius: 50%; background-color: #4CAF50; position: absolute; transform-origin: left center; transform: rotate(60deg); /* 模擬進(jìn)度 */ } </style>
- .circle-progress 是容器,定義圓形的基本樣式。
- .circle-progress-mask 用于裁剪圓形,分成左右兩半。
- .circle-progress-fill 是填充的顏色,通過(guò) transform: rotate() 旋轉(zhuǎn),實(shí)現(xiàn)進(jìn)度效果。
- transform-origin: left center 設(shè)置旋轉(zhuǎn)中心點(diǎn)。
-
優(yōu)點(diǎn): 代碼簡(jiǎn)潔,易于理解。
-
缺點(diǎn): 靈活性較差,難以實(shí)現(xiàn)復(fù)雜的動(dòng)畫效果。只能實(shí)現(xiàn)半圓以上的進(jìn)度。
如何動(dòng)態(tài)更新進(jìn)度環(huán)的進(jìn)度?
無(wú)論是 SVG 還是 CSS 方式,動(dòng)態(tài)更新進(jìn)度都需要 JavaScript 的配合。
- SVG: 通過(guò) JavaScript 獲取 SVG 元素,修改 stroke-dashoffset 屬性的值。
- CSS: 通過(guò) JavaScript 獲取 CSS 元素,修改 transform: rotate() 的角度值。
核心思路是,根據(jù)進(jìn)度百分比,計(jì)算出對(duì)應(yīng)的 stroke-dashoffset 或旋轉(zhuǎn)角度,然后更新元素的樣式。
如何添加進(jìn)度文字顯示?
可以在進(jìn)度環(huán)的中心添加文字,顯示當(dāng)前的進(jìn)度百分比。
- SVG: 在 SVG 中添加
元素,設(shè)置其 x 和 y 屬性,使其居中顯示。 - CSS: 在 .circle-progress 容器中添加一個(gè) 元素,使用 position: absolute 和 transform: translate() 使其居中顯示。
然后,通過(guò) JavaScript 更新文字內(nèi)容,顯示當(dāng)前的進(jìn)度百分比。
如何兼容不同的瀏覽器?
- SVG: SVG 的兼容性較好,主流瀏覽器都支持。
- CSS: clip 屬性在一些老版本瀏覽器中可能存在兼容性問(wèn)題??梢允褂闷渌椒ǎ?mask-image,但兼容性也需要考慮。
建議在開發(fā)過(guò)程中,進(jìn)行充分的測(cè)試,確保在不同瀏覽器中都能正常顯示。可以使用 Autoprefixer 自動(dòng)添加瀏覽器前綴,提高兼容性。