【问题标题】:How to animate a car's wheels based on a moving and stopping car using CSS?如何使用 CSS 根据移动和停止的汽车为汽车的车轮设置动画?
【发布时间】:2019-11-22 17:32:34
【问题描述】:

我正在尝试使用 CSS 为汽车制作动画。我已经成功地为车轮和汽车制作了动画。

汽车驶入并停下,然后驶离。此动画循环播放。

现在,当汽车停下时,我也需要停止车轮。但我似乎无法做到这一点。

这是我目前所拥有的:

@keyframes wheel{
 0%{
   transform: rotate(0deg)
  }
  
    35% {
    transform: rotate(-90deg)
  }
  36%,
  56% {
    transform: rotate(-180deg)
  }
  
 100%{
   transform: rotate(-359deg)
  }
}

@keyframes moving {
  0% {
    right: -80em;
    animation-timing-function: ease-out;
  }
  35% {
    right: 0;
  }
  36%,
  56% {
    right: 0;
    animation-timing-function: ease-in;
  }
  100% {
    right: 120%;
  }
}

@keyframes stableWheel {
  from {transform: translateY(-.0em);}
  to {transform: translateY(-.0em);}
}


.car{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  margin: 0 auto;
  position: relative;
  width: 600px;
  height:271px;
  overflow:hidden;
    animation: moving 10s linear -2s infinite;
}
.carbody{
  animation: carmove 3.1s infinite linear;
  background: url('https://i.stack.imgur.com/xWNOG.png') 0 0;
  background-size: cover;
  height: 271px;
  position: relative; 
  width: 600px;
  z-index: 125;
}



.weel{

  animation: wheel 0.7s infinite linear;
  background: url('https://i.stack.imgur.com/0Osjx.png') 0 0;
  height: 85px;
  position: absolute;
  top: 67%;
  width: 85px;
  z-index: 200;
}
.weel1{left: 85px;}
.weel2{left: 454px;}

/*animations*/
@keyframes carmove{
  0%{transform: translateY(0px);}
  25%{transform: translateY(1px)}
  29%{transform: translateY(2px)}
  33%{transform: translateY(3px)}
  47%{transform: translateY(0px)}
  58%{transform: translateY(1px)}
  62%{transform: translateY(2px)}
  66%{transform: translateY(3px)}
  75%{transform: translateY(1px)}
  100%{transform: translateY(0px)}
}




body {
  -webkit-animation: color-fade 10s infinite;
  -moz-animation: color-fade 10s infinite;
  animation: color-fade 10s infinite;
}

@-webkit-keyframes color-fade {
  0% { background: #9a5342; }
  25% { background: #fffc0c; }
  50% { background: #e46d00; }
  75% { background: #ff3506; }
  100% { background: #9a5342; }
}


.stopedWeel{
     animation: stableWheel .2s linear infinite alternate;
  
}
<div class="car">
  <div class="carbody"></div>
  <div class="weel weel1"></div>
  <div class="weel weel2"></div>
</div>

车轮动画是这样的:

@keyframes wheel{
     0%{
       transform: rotate(0deg)
      }

        35% {
        transform: rotate(-90deg)
      }
      36%,
      56% {
        transform: rotate(-180deg)
      }

     100%{
       transform: rotate(-359deg)
      }
    }

如果你运行我的代码,轮子都是紧张和滞后的。

有人可以就此提出建议吗?

【问题讨论】:

    标签: html css css-animations


    【解决方案1】:

    为方便起见,请对两个动画使用相同的持续时间,然后增加旋转角度来控制滚轮。只需确保您在结束时返回n*360deg(在这种情况下不是强制性的,因为汽车运动中没有循环)

    我还优化了您的代码以使用百分比值,因此您只需调整主要元素的宽度即可轻松控制整车:

    .car{
      margin: 0 auto;
      position: relative;
      width: 400px;
      animation: moving 10s linear -2s infinite;
    }
    .car:before {
      content:"";
      display:block;
      animation: carmove 3.1s infinite linear;
      background: url('https://i.stack.imgur.com/xWNOG.png') center/cover;
      padding-top:45.25%;
    }
    
    .weel{
      animation: wheel 10s infinite -2s linear;
      background: url('https://i.stack.imgur.com/0Osjx.png') center/cover;
      position: absolute;
      bottom:0.8%;
      width: 14.15%;
    }
    .weel:before {
       content:"";
       display:block;
       padding-top:100%;
    }
    .weel1{left: 14.5%;}
    .weel2{right: 10%;}
    
    /*animations*/
    @keyframes carmove{
      0%{transform: translateY(0px);}
      25%{transform: translateY(1px)}
      29%{transform: translateY(2px)}
      33%{transform: translateY(3px)}
      47%{transform: translateY(0px)}
      58%{transform: translateY(1px)}
      62%{transform: translateY(2px)}
      66%{transform: translateY(3px)}
      75%{transform: translateY(1px)}
      100%{transform: translateY(0px)}
    }
    
    @keyframes wheel{
     0%{
       transform: rotate(0deg)
      }
      35% {
        transform: rotate(-920deg)
      }
      36%,
      56% {
        transform: rotate(-920deg)
      }
     100%{
       transform: rotate(-1440deg)
      }
    }
    
    @keyframes moving {
      0% {
        right: -80em;
        animation-timing-function: ease-out;
      }
      35% {
        right: 0;
      }
      36%,
      56% {
        right: 0;
        animation-timing-function: ease-in;
      }
      100% {
        right: 120%;
      }
    }
    
    body {
      overflow:hidden;
    }
    <div class="car">
      <div class="weel weel1"></div>
      <div class="weel weel2"></div>
    </div>

    还有一辆更小的汽车:

    .car{
      margin: 0 auto;
      position: relative;
      width: 150px;
      animation: moving 10s linear -2s infinite;
    }
    .car:before {
      content:"";
      display:block;
      animation: carmove 3.1s infinite linear;
      background: url('https://i.stack.imgur.com/xWNOG.png') center/cover;
      padding-top:45.25%;
    }
    
    .weel{
      animation: wheel 10s infinite -2s linear;
      background: url('https://i.stack.imgur.com/0Osjx.png') center/cover;
      position: absolute;
      bottom:0.8%;
      width: 14.15%;
    }
    .weel:before {
       content:"";
       display:block;
       padding-top:100%;
    }
    .weel1{left: 14.5%;}
    .weel2{right: 10%;}
    
    /*animations*/
    @keyframes carmove{
      0%{transform: translateY(0px);}
      25%{transform: translateY(1px)}
      29%{transform: translateY(2px)}
      33%{transform: translateY(3px)}
      47%{transform: translateY(0px)}
      58%{transform: translateY(1px)}
      62%{transform: translateY(2px)}
      66%{transform: translateY(3px)}
      75%{transform: translateY(1px)}
      100%{transform: translateY(0px)}
    }
    
    @keyframes wheel{
     0%{
       transform: rotate(0deg)
      }
      35% {
        transform: rotate(-920deg)
      }
      36%,
      56% {
        transform: rotate(-920deg)
      }
     100%{
       transform: rotate(-1440deg)
      }
    }
    
    @keyframes moving {
      0% {
        right: -80em;
        animation-timing-function: ease-out;
      }
      35% {
        right: 0;
      }
      36%,
      56% {
        right: 0;
        animation-timing-function: ease-in;
      }
      100% {
        right: 120%;
      }
    }
    
    body {
      overflow:hidden;
    }
    <div class="car">
      <div class="weel weel1"></div>
      <div class="weel weel2"></div>
    </div>

    【讨论】:

      【解决方案2】:

      SVG 动画

      场景

      1. 汽车出现在右侧。
        车轮旋转

      2. 汽车停在路中间。
        车轮不转

      3. 车又开了。
        车轮旋转

      4. 整个循环的重复

      我为什么要编写这个原始算法?只是为了在启动并行和顺序动画的顺序上清楚地遵循它。

      与 SVG 中的 CSS 动画不同,您无需费力的时间计算即可完成。 并且因为它是在场景中编写的,并且实现了开始,停止动画

      例如:

      • 在第一条腿的动作结束后暂停

      begin="an_move1.end"

      • 暂停后将开始第二段路径的移动

      begin="an_pause.end"

      更多解释参见代码 cmets。

      body {
        -webkit-animation: color-fade 10s infinite;
        -moz-animation: color-fade 10s infinite;
        animation: color-fade 10s infinite;
      }
      
      @-webkit-keyframes color-fade {
        0% { background: #9a5342; }
        25% { background: #fffc0c; }
        50% { background: #e46d00; }
        75% { background: #ff3506; }
        100% { background: #9a5342; }
      }
      
      .container {
      width:100%;
      height:100%;
      }
      <div class="container">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
          xmlns:xlink="http://www.w3.org/1999/xlink"
             width="1200" height="600" viewBox="0 0 600 800" preserveAspectRatio="xMinYMin meet">  
        <!-- Car -->
      <g id="car" transform="translate(-2400,0)">
      <image xlink:href="https://i.stack.imgur.com/xWNOG.png" width="100%" height="100%" />
      
       <g id="wheel_left" transform=" translate(85,430) scale(0.145)" >
         <image xlink:href="https://i.stack.imgur.com/0Osjx.png" width="100%" height="100%" >
           <!-- Left wheel rotation animation -->
       <animateTransform
           id="an_left"
      	 attributeName="transform"
      	 type="rotate"
      	 begin="0s;6s;16s;26s;36s;46s;56s"
      	 end="an_pause.begin"
      	 values="
      	 0 300 400;
      	-360 300 400"
      	 dur="1s"
      	 repeatCount="indefinite"
       />
       </image>
       </g>  
           
       <g id="wheel_right" transform=" translate(455,430) scale(0.145)" >
       <image xlink:href="https://i.stack.imgur.com/0Osjx.png" width="100%" height="100%" > 
           <!-- Right wheel rotation animation -->
          <animateTransform
      	  id="an_right"
      	  attributeName="transform"
      	  type="rotate"
      	  begin="0s;6s;16s;26s;36s;46s;56s;66s;76s"
      	  end="an_pause.begin"
      	  values="
      	   0 300 400;
      	-360 300 400"
      	  dur="1s"
      	  repeatCount="indefinite"
      	/>
       </image>
       </g>
      </g>   
      <!-- Animation of a car moving to a stop -->
       <animateTransform
         id="an_move1"
         xlink:href="#car"
         attributeName="transform"
         type="translate"
         begin="0s;an_move2.end"
         values="2400;800"
         dur="4s"
         fill="freeze"
         repeatCount="1"
       />  
       <!-- Car  move  pause  -->
       <animateTransform
         id="an_pause"
         xlink:href="#car"
         attributeName="transform"
         type="translate"
         begin="an_move1.end"
         values="800"
         dur="2s"
         fill="freeze"
         repeatCount="1"
       /> 
       <!-- Animation of a car moving after a stop -->
        <animateTransform
         id="an_move2"
         xlink:href="#car"
         attributeName="transform"
         type="translate"
         begin="an_pause.end"
         values="800;-600"
         dur="4s"
         fill="freeze"
         repeatCount="1"
       /> 
      
      </svg>	 
      </div>

      【讨论】:

        【解决方案3】:

        在视图屏幕的中间,汽车和车轮动画都应该停止几秒钟。所以车轮动画应该和汽车的动画一样。

        .car{
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          margin: 0 auto;
          position: relative;
          width: 600px;
          height:271px;
          overflow:hidden;
          animation: moving 10s linear -2s infinite;
        }
        

        更新后的车轮样式应如下所示。

        .weel{
          /*animation: wheel 0.7s infinite linear;*/
          animation: wheel 10s linear -2s infinite;
        
          background: url('https://i.stack.imgur.com/0Osjx.png') 0 0;
          height: 85px;
          position: absolute;
          top: 67%;
          width: 85px;
          z-index: 200;
        }
        

        如果您现在运行代码,您可以看到车轮和汽车动画同时工作。但是在那里你可以发现车速和轮速之间的差异,并且轮子应该停止直到车身再次移动。为了解决速度和车轮问题,您需要增加关键帧值,同时保持中间两个关键帧值相等。

        @keyframes wheel{
        0%{
           transform: rotate(0deg)
          }
        
            35% {
            transform: rotate(-500deg)
          }
          36%,
          56% {
            transform: rotate(-500deg)
          }
        
         100%{
           transform: rotate(-1000deg)
          }
        }
        

        Js Fiddle link 显示实时动画。

        【讨论】:

        • 我的意思是您给出的解决方案/想法几乎与我在回答中给出的相同。这和我的有什么不同?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多