代码之家  ›  专栏  ›  技术社区  ›  Borja

如何像速度表一样将背景色旋转半圈

  •  1
  • Borja  · 技术社区  · 7 年前

    CSS3 我会像图表一样使用它。 我的问题是我想动画 background-color rotate 它。

    这是jsfiddle https://jsfiddle.net/wu1tbsne/28/

    //FIRST BAR
    $('#half-donut').addClass('first-start');
    
    setTimeout(function() {
      $('#half-donut').addClass('first-pause');
    }, 1700);
    body {
      background-color: #000;
    }
    
    
    #half-donut {
        width: 200px;
        height: 100px;
        background-color: #fff;
        border-radius: 200px 200px 0% 0%;
        margin-right: 3px;
        display: flex;
        justify-content: center;
        align-items: flex-end;
    }
    
    
    #figlio {
        width: 140px;
        height: 75px;
        background: #000;
        border-radius: 140px 140px 0% 0%;
    }
    
    
    @keyframes first {
      0% {
        background-color: green;
      }
      33% {
        background-color: yellow; 
      }
      66% {
        background-color: orange; 
      }
      100% {
        background-color: red; 
      }
    }
    
    
    .first-start {
      animation: first 2s linear;
    }
    
    .first-pause {
      animation-play-state: paused;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="half-donut">
        <div id="figlio"></div>
    </div>

    最后我想看到这样的事情:

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  2
  •   Borja    7 年前

    解决 这个问题现在很有效:

    这是代码:

    $('#first').addClass('first-start');
    
    setTimeout(function() {
      $('#first').addClass('first-pause');
    }, 1700);
    
    
    //SECOND BAR
    $('#second').addClass('second-start');
    
    setTimeout(function() {
      $('#second').addClass('second-pause');
    }, 400);
    #page {
      margin-top: 50px;
      width: 300px;
      height: 300px;
      background-color: #000;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      z-index: 4;
      overflow: hidden;
    }
    
    #box-first,
    #box-second {
      width: 200px;
      height: 100px;
      background-color: #fff;
      border-radius: 200px 200px 0 0;
      margin-top: 10px;
      margin-bottom: 10px;
      position: relative;
      display: flex;
      justify-content: flex-end;
      align-items: flex-start;
      z-index: 3;
      overflow: hidden;
    }
    
    #first,
    #second {
      border-radius: 200px 200px 0 0;
      margin: 0;
      background: red;
      width: 200px;
      height: 100px;
      transform: rotate(180deg);
      transform-origin: 50% 100%;
      position: absolute;
      top: 0px;
      right: 0px;
      border: 0;
      z-index: 1;
    }
    
    #n1,
    #n2 {
      font-size: 20px;
      color: #fff;
      font-weight: bold;
      position: absolute;
      left: 50px;
      right: 0;
      text-align: center;
      top: 50px;
      bottom: 0;
      display: flex;
      align-items: flex-end;
      justify-content: center;
      width: 100px;
      height: 50px;
      background: #000;
      border-radius: 100px 100px 0 0;
      z-Index: 1;
      overflow: hidden;
    }
    
    
    
    
    @keyframes first {
      0% {
        background-color: green;
        transform: rotate(180deg);
      }
      33% {
        background-color: yellow;
        transform: rotate(240deg);
      }
      66% {
        background-color: orange;
        transform: rotate(300deg);
      }
      100% {
        background-color: red;
        transform: rotate(360deg);
      }
    }
    
    @keyframes second {
      0% {
        background-color: green;
        transform: rotate(180deg);
      }
      33% {
        background-color: yellow;
        transform: rotate(240deg);
      }
      66% {
        background-color: orange;
        transform: rotate(300deg);
      }
      100% {
        background-color: red;
        transform: rotate(360deg);
      }
    }
    
    
    .first-start,
    .second-start {
      animation: first 2s linear;
    }
    
    .first-pause,
    .second-pause {
      animation-play-state: paused;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="page">
      <div id="box-first">
        <div id="first">
    
        </div>
        <div id="n1">
          1500
        </div>
      </div>
      <div id="box-second">
        <div id="second">
    
        </div>
        <div id="n2">
          270
        </div>
      </div>
    </div>

    https://jsfiddle.net/k85t9zgq/22/

        2
  •  0
  •   95faf8e76605e973    7 年前

    这是一种方法。你可以尝试用一个元素来隐藏原始的div,这个元素可以设置你想要的动画。

    在下面的示例中,我只使用了psuedo ::before #half-donut

    $(document).ready(function(){
    
    $('#half-donut').addClass('first-start');
    $('#half-donut::before').addClass('start-anim');
    
     setTimeout(function() {
       $('#half-donut').addClass('first-pause');
     }, 1700);
    
    });
    body {
      background-color: #000;
    }
    
    #half-donut {
        z-index: 0;
        position:relative;
        width: 200px;
        height: 100px;
        background-color: #fff;
        border-radius: 200px 200px 0% 0%;
        margin-right: 3px;
        display: flex;
        justify-content: center;
        align-items: flex-end;
    }
    
    #half-donut::before {
        position: absolute;
        z-index: 1;
        top: 0;
        right: -20%;
        overflow: hidden;
        content: "";
        background-color: black;
        width: 100%;
        height: 100%;
        margin-right: 3px;
        display: flex;
        justify-content: center;
        align-items: flex-end;
        animation: start 2s linear;
    }
    
    
    #figlio {
        z-index: 2;
        width: 140px;
        height: 75px;
        background: #000;
        border-radius: 140px 140px 0% 0%;
    }
    
    
    @keyframes start {
      0% {
        right: -20%;
      }
      33% {
        right: -33%; 
      }
      66% {
        right: -66%;
      }
      100% {
        right: -100%;
      }
    }
    
    @keyframes first {
      0% {
        background-color: green;
      }
      33% {
        background-color: yellow; 
      }
      66% {
        background-color: orange; 
      }
      100% {
        background-color: red; 
      }
    }
    
    .first-start {
      animation: first 2s linear;
    }
    
    .start-anim {
      animation: start 2s linear;
    }
    
    .first-pause {
      animation-play-state: paused;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="half-donut">
        <div id="figlio"></div>
    </div>
        3
  •  0
  •   importantaccount1    7 年前

    我的解决方案如下:

    https://codebrace.com/editor/b01ecd3e6

    body {
      background-color: #000;
    }
    
    
    #half-donut {
        width: 200px;
        height: 100px;
        background-color: #0f0;
        border-radius: 200px 200px 0% 0%;
        margin-right: 3px;
        display: flex;
        justify-content: center;
        align-items: flex-end;
        position:relative;
        overflow: hidden;
    }
    #meterptr{
        width:200px;
        height:200px;
        position: absolute;
        background-color:red;
        top:0;
        left:-50%;
        transform-origin: center right;
        transform: rotate(-90deg);
    }
    #figlio {
        width: 140px;
        height: 75px;
        background: #000;
        border-radius: 140px 140px 0% 0%;
        position:absolute;
    }
    
    推荐文章