Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sliding CSS Animation not flipping in correct area

I am trying to create a CSS animation that has a bird slide to the right, flip and slide back to the left and flip again. And repeat... Like the bird is running back and forth.

Right now it flips, slides and doesn't like like it should. Here is the CodePen

Here is my HTML:

<div class="fifth">
 <div id="GoRight">
   <p>... Loading</p>
 </div>
 <div id="TurnGoLeft"></div>

Here is my CSS:

    .fifth h2 {
    margin-top: 130px;
}
.fifth #GoRight{
  width: 200px;
  height: 5px;
  position: fixed;
  margin-left: 200px;
  margin-top: 14px;
}
.fifth #GoRight p{
    font-size: 20px;
    color: #1886c7;
    font-family: "Effra Regular", "Droid Sans", Arial, serif;
}
.fifth #TurnGoLeft {
  width: 110px; 
  height: 66px;
  background-image: url("http://goo.gl/BaIPWx");
  background-repeat: no-repeat;
  position: fixed;
  left: 60px;
  top: 10px;
  -webkit-animation: slideflip, flipslide 2s infinite alternate;
}
@-webkit-keyframes slideflip {
  0% {
    margin-left: 0%;
    width: 110px; 
  }
  100% {
    margin-left: 20%;
    width: 110px;
  }
}
@-webkit-keyframes flipslide {
  50% {
    margin-left: 0%;
    width: 110px; 
  }
  100% {
    -webkit-transform: rotateY(180deg);
    margin-left: 20%;
    width: 110px;
  }
}

Any help or insight would be really helpful! Thanks!

like image 509
Andrew C Avatar asked Dec 19 '14 15:12

Andrew C


People also ask

Why is my CSS animation not working?

CSS animations work on most modern mobile and desktop browsers. However, your animations may not work if you're using an older browser or a version of your browser that hasn't been updated in several years, simply due to lack of browser support.

What is the CSS 3 rule that allows for animations?

The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.

Which CSS animation property specifies the element style when the animation is not taking place?

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both). CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.

How do I move an animation from left to right in CSS?

To do that we need to change the transform around it's y-axis. Note that if we make it turn around only at 50% it will slowly turn around at the same time it's moving. So we need to specify how long the potato should be at -webkit-transform: rotateY(0deg); .


1 Answers

The way you can solve it is not using alternate and instead create just one animation and take some steps to do the left to right and the flip:

@-webkit-keyframes slideflip {
  0% {
    margin-left: 0%;
    -webkit-transform: rotateY(0);
  }
  25% {
    margin-left: 20%;
    -webkit-transform: rotateY(0);
  }
  45% {
    margin-left: 20%;
    -webkit-transform: rotateY(180deg);
  }
  80% {
    margin-left: 0;
    -webkit-transform: rotateY(180deg);
  }
}

CodepenDemo


Codepen and amount of % changed from the posted one on my commentary, based on the solution of @Ruddy that helps to avoid the flip at the first step of loaded the animation

like image 118
DaniP Avatar answered Oct 05 '22 00:10

DaniP