利用css3實(shí)現(xiàn)進(jìn)度條效果及動(dòng)態(tài)添加百分比

項(xiàng)目過程中,開始使用了js的requestAnimationFrame方法實(shí)現(xiàn)進(jìn)度條,但是在數(shù)據(jù)超級(jí)多的時(shí)候非常影響性能,如此改用css去實(shí)現(xiàn),優(yōu)化。
先貼代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> *{margin: 0;padding: 0} .box{width: 100px;height: 10px;border-radius: 10px;background: #999;margin: 100px auto;border: 1px solid #ff6780;} .child{position: relative;height:100%;border-radius:inherit;} .process-animate{background: #ff6780;position: absolute;left: 0;top: 0;bottom: 0;border-radius:inherit; animation: process 1s linear forwards ; } @keyframes process { 0%{ left:0;right:100%; } 20%{ right:80% } 40%{ right:60%; } 60%{right:40%;} 80%{right:20%;} 100%{right:0;} } </style> </head> <body> <div class="box"> <div class="child" style="width:50%"> // child的百分比就是進(jìn)度條的占比效果 <p class="process-animate"></p> </div> </div> </body> </html>
效果圖(復(fù)制代碼可查看動(dòng)態(tài)效果):
正常情況下,百分比肯定是根據(jù)后臺(tái)數(shù)據(jù)進(jìn)行計(jì)算得出的,所以是動(dòng)態(tài)傳入的,下面貼vue代碼
進(jìn)度條子組件(progress.vue):
<template> <div class="process-wrapper" :class="{'addGray':addGray}"> <div class="process-child" ref="processChild"> <p class="process-animate" :class="{'addGray':addGray}"></p> </div> </div> </template> <script> export default { props: { addGray: { //置灰 type: Boolean, default: false }, progressWidth: { //進(jìn)度條百分比 type: Number, default: 0 } }, mounted() { this.$nextTick(() => { console.log(this.addGray, "addGray---"); this.$refs.processChild.style.width = this.progressWidth + "%"; //動(dòng)態(tài)改變進(jìn)度條 // this.$refs.processChild.style.width = 90 + "%"; 測(cè)試效果 }); } }; </script> <style lang="scss" scoped> .process-wrapper { width: 1.98rem; height: 0.13rem; margin: 0.12rem 0 0.1rem 0; border-radius: 0.1rem; background: #fff; border: 0.01rem solid #ff6780; &.addGray { background: #999; border: 0.01rem solid #999; } .process-child { position: relative; height: 100%; // width: 100%; //這個(gè)width就是動(dòng)態(tài)變化。通過js改變 border-radius: inherit; .process-animate { background: #ff6780; position: absolute; left: 0; top: 0; bottom: 0; border-radius: inherit; animation: process 1s linear forwards; &.addGray { background: #999 !important; // border: 0.01rem solid #999; } } } } @keyframes process { 0% { left: 0; right: 100%; } 20% { right: 80%; } 40% { right: 60%; } 60% { right: 40%; } 80% { right: 20%; } 100% { right: 0; } } </style>
父組件調(diào)用:
<!-- 進(jìn)度條 --> <Progress :addGray="inactive" :progressWidth="progressWidth"></Progress>
看看實(shí)際效果:
over;完美用css 解決了js遞歸動(dòng)畫性能消耗。
到此這篇關(guān)于利用css3實(shí)現(xiàn)進(jìn)度條效果及動(dòng)態(tài)添加百分比的文章就介紹到這了,更多相關(guān)css3進(jìn)度條添加動(dòng)態(tài)百分比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
css 實(shí)現(xiàn)圓形漸變進(jìn)度條效果的示例代碼
這篇文章主要介紹了css 實(shí)現(xiàn)圓形漸變進(jìn)度條效果的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-24css 橫向進(jìn)度條和豎向進(jìn)度條實(shí)現(xiàn)代碼
這篇文章主要介紹了css 橫向進(jìn)度條和豎向進(jìn)度條實(shí)現(xiàn)代碼,有時(shí)候看一些不錯(cuò)的滾動(dòng)條效果不錯(cuò),這里給大家分享一下如果用css實(shí)現(xiàn)2020-04-14使用CSS3實(shí)現(xiàn)環(huán)形進(jìn)度條效果
這篇文章主要介紹了使用CSS3實(shí)現(xiàn)環(huán)形進(jìn)度條效果,需要的朋友可以參考下2018-06-01css 進(jìn)度條的文字根據(jù)進(jìn)度漸變的示例代碼
這篇文章主要介紹了css 進(jìn)度條的文字根據(jù)進(jìn)度漸變的示例代碼,介紹了進(jìn)度條里面的文字需要根據(jù)進(jìn)度的長(zhǎng)度而變化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一2018-01-09手把手教你用CSS實(shí)現(xiàn)帶箭頭的流程進(jìn)度條
這篇文章主要給大家介紹了利用CSS實(shí)現(xiàn)帶箭頭的流程進(jìn)度條大方法,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,有需要的朋友們一起來看看吧。2017-01-22CSS實(shí)現(xiàn)進(jìn)度條和訂單進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了CSS進(jìn)度條和訂單進(jìn)度條的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-12- 純css做漂亮好看的進(jìn)度條,看了絕對(duì)不后悔。2010-05-31
- [html] <style> #graphbox{ border:1px solid #e7e7e7; padding:10px; width:250px; background-color:#f8f8f8; margin:5px 0; } #graphbox h2{ color:#662009-03-30
僅僅使用 HTML/CSS 實(shí)現(xiàn)各類進(jìn)度條的方式匯總
這篇文章主要介紹了僅僅使用 HTML/CSS 實(shí)現(xiàn)各類進(jìn)度條的方式匯總,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-08