简体   繁体   English

CSS动画在返回时旋转

[英]CSS Animation rotate on return

I am trying to create animation with this arrow that will move up and down and also rotate or change its direction on 0% and 100% . 我正在尝试使用此箭头创建动画,该箭头将上下移动并在0%100%上旋转或更改其方向。 But what I got so far is that arrow will rotate for sec on 100% and then return back to previous direction. 但到目前为止我得到的是箭头将在100%旋转几秒然后返回到前一个方向。 So basically what I am trying to do is, arrow should point up on its way up and point down on return or on its way down . 所以基本上我所要做的是,箭头应的道路点了 起来点向下的申报或者其一路下跌 Can I do this with just CSS animations? 我可以用CSS动画做到这一点吗?

DEMO DEMO

On way up arrow should have this direction 向上箭头应该有这个方向 在此输入图像描述 and on way down this one 并在这个下来的路上 在此输入图像描述 Also I want to animate that rotation on 100% and 0% with rotate if its possible. 此外,我想动画上旋转100%0%rotate ,如果可能。

Just Change the animation with below code 只需使用以下代码更改动画即可

@keyframes bounceAndRotate {
  0% {
    transform:rotate(0deg);top:-20px;
  }
  25% {
     transform:rotate(00deg);top:-10px;
  }
  50% {
     transform:rotate(0deg);top:0px;
  }
  75% {
     transform:rotate(180deg);top:10px;
  }
  100% {
    transform: rotate(180deg);top:20px;
  }
}

For Demo CLICK HERE For Demo 对于Demo 点击这里进行演示

Just do away with the alternating direction setting and adjust the keyframe settings such that for the first 50% duration the upward movement and spin is done and then for the next 50% it comes down with the arrow facing downwards. 只需取消交替方向设置并调整关键帧设置,使得在前50%持续时间内完成向上移动和旋转,然后在接下来的50%持续时,箭头朝下。 The animation-duration should be doubled because the alternate effect is produced by the keyframes itself. animation-duration应加倍,因为替代效果是由关键帧本身产生的。

But doing this would spoil one part of your current animation where the arrow spins to face downward and then spins back to face upwards. 但这样做会破坏当前动画的一部分,箭头旋转面向下,然后旋转回面朝上。 Also removing alternate would make the reverse rotation on 100% not happen. 同时移除alternate将使100%的反向旋转不会发生。 But that can be overcome by adding an extra keyframe. 但是可以通过添加额外的关键帧来克服这个问题。 This extra keyframe must mirror the initial position of the element (which is no rotation). 这个额外的关键帧必须镜像元素的初始位置(不是旋转)。

 .arrow { height: 50px; position: relative; width: 10px; background: black; margin: 100px; display: inline-block; animation: bounceAndRotate 2s infinite linear; } .arrow:before { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(50deg); left: -10px; } .arrow:after { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(-50deg); right: -10px; } @keyframes bounceAndRotate { 0% { transform: translateY(0) rotate(0deg); } 12.5% { transform: translateY(-10px); } 25% { transform: translateY(-20px); } 37.5% { transform: translateY(-30px); } 50% { transform: translateY(-40px) rotate(180deg); } 90% { transform: translateY(0px) rotate(180deg); } 100% { transform: translateY(0px); } } 
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet" /> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div class="arrow"></div> 


If you want that part also to be maintained before the downward move then add extra keyframes like in the below snippet. 如果您希望在向下移动之前维护该部分,则添加额外的关键帧,如下面的代码段所示。 Here the sequence of events is as follows: 这里的事件顺序如下:

  • Arrow goes upwards with arrow head pointing upwards. 向上箭头向上箭头指向上方。
  • When it reaches the topmost point, it rotates 180deg such that the arrow head points downwards. 当它到达最高点时,它旋转180度,使箭头指向下方。
  • Once the rotation is complete, it again rotates back to its original position (arrow head pointing up while maintaining the position) 旋转完成后,它再次旋转回原位(箭头朝上,同时保持位置)
  • Once this is complete, it again rotates 180deg such that the arrow head points downwards. 一旦完成,它再次旋转180度,使箭头指向下方。
  • Arrow moves downwards with the arrow head pointing downwards. 箭头向下移动,箭头指向下方。

 .arrow { height: 50px; position: relative; width: 10px; background: black; margin: 100px; display: inline-block; animation: bounceAndRotate 2s infinite linear; } .arrow:before { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(50deg); left: -10px; } .arrow:after { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(-50deg); right: -10px; } @keyframes bounceAndRotate { 0% { transform: translateY(0) rotate(0deg); } 12.5% { transform: translateY(-10px); } 25% { transform: translateY(-20px); } 37.5% { transform: translateY(-30px); } 45% { transform: translateY(-40px) rotate(180deg); } 55%, 60%{ transform: translateY(-40px); } 67.5%{ transform: translateY(-40px) rotate(180deg); } 90% { transform: translateY(0px) rotate(180deg); } 100% { transform: translateY(0px); } } 
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet" /> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div class="arrow"></div> 

You can do like following way: 你可以这样做:

 .arrow { height: 50px; position: relative; width: 10px; background: black; margin: 100px; display: inline-block; animation: bounceAndRotate 2s infinite linear alternate; } .arrow:before { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(50deg); left: -10px; } .arrow:after { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(-50deg); right: -10px; } @keyframes bounceAndRotate { 0% { transform: translateY(10px) rotate(0deg); } 20% { transform: translateY(-20px); } 50% { transform: translateY(-40px) rotate(180deg); } 60% { transform: translateY(-30px) rotate(180deg); } 80% { transform: translateY(-20px) rotate(180deg); } 100% { transform: translateY(0px) rotate(180deg); } } 
 <div class="arrow"> </div> 

Hope it helps. 希望能帮助到你。 Working Fiddle 工作小提琴

Edit: 编辑:

And if you want 360 degree rotation then do like following by removing alternative: 如果您想要360度旋转,那么通过删除替代方案来执行以下操作:

 .arrow { height: 50px; position: relative; width: 10px; background: black; margin: 100px; display: inline-block; animation: bounceAndRotate 2s infinite linear; } .arrow:before { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(50deg); left: -10px; } .arrow:after { content: " "; width: 10px; background: black; height: 35px; position: absolute; top: -10px; transform: rotate(-50deg); right: -10px; } @keyframes bounceAndRotate { 0% { transform: translateY(10px) rotate(0deg); } 20% { transform: translateY(-20px); } 50% { transform: translateY(-40px) rotate(180deg); } 60% { transform: translateY(-30px) rotate(180deg); } 80% { transform: translateY(-20px) rotate(180deg); } 100% { transform: translateY(0px) rotate(360deg); } } 
 <div class="arrow"> </div> 

Thanks for all anwsers, but this is what i ended up with 感谢所有的冬天,但这就是我最终的结果

DEMO DEMO

 @keyframes bounceAndRotate {
  0% {
    transform: translateY(0px) rotate(0deg);
  }

  35% {
    transform: translateY(-195px) rotate(0deg);
  }

  50% {
    transform:translateY(-200px) rotate(180deg);
  }

  85% {
    transform: translateY(-5px) rotate(180deg);
  }

   100% {
    transform: translateY(0px)  rotate(0deg);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM