CSS3에서 진행률 표시줄 효과를 얻는 방법

Guanhui
풀어 주다: 2020-06-24 13:43:38
앞으로
3003명이 탐색했습니다.

CSS3에서 진행률 표시줄 효과를 얻는 방법

프로젝트 진행 중에 진행률 표시줄 구현을 위해 js의 requestAnimationFrame 메소드를 사용하기 시작했는데 데이터가 많을 때 성능에 큰 영향을 미치기 때문에 CSS로 전환하여 구현하고 최적화했습니다.

먼저 코드 게시:

<!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>
    <p class="box">
        <p class="child" style="width:50%"> // child的百分比就是进度条的占比效果
            <p class="process-animate"></p>
        </p>
    </p>
</body>
</html>
로그인 후 복사

렌더링(동적 효과를 보려면 코드 복사):

일반적인 상황에서는 백분율을 배경 데이터를 기반으로 계산해야 하므로 동적으로 Post에 전달됩니다. vue 코드 아래에 있습니다

진행 표시줄 하위 구성 요소(progress.vue):

<template>
  <p class="process-wrapper" :class="{&#39;addGray&#39;:addGray}">
    <p class="process-child" ref="processChild">
      <p class="process-animate" :class="{&#39;addGray&#39;:addGray}"></p>
    </p>
  </p>
</template>
 
<script>
export default {
  props: {
    addGray: {
      //置灰
      type: Boolean,
      default: false
    },
    progressWidth: {
      //进度条百分比
      type: Number,
      default: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      console.log(this.addGray, "addGray---");
      this.$refs.processChild.style.width = this.progressWidth + "%";  //动态改变进度条
      // this.$refs.processChild.style.width = 90 + "%"; 测试效果
    });
  }
};
</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%;  //这个width就是动态变化。通过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>
로그인 후 복사

부모 구성 요소 호출:

<!-- 进度条 -->
 <Progress :addGray="inactive" :progressWidth="progressWidth"></Progress>
로그인 후 복사

실제 효과 보기:

추천 튜토리얼: "CSS Tutorial"

위 내용은 CSS3에서 진행률 표시줄 효과를 얻는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:jb51.net
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!