如何在div中更改动画方向

时间:2019-07-15 09:28:43

标签: html css animation

我创建了一个div,其动画方向在右侧,但我希望其中的图像保持直立并且不会移动。 问题在于图像正在获得主格的方向。

#loader {
  /* Uncomment this to make it run! */
  /*
     animation: loader 5s linear infinite; 
  */
  position: absolute;
  top: calc(50% - 20px);
  left: calc(50% - 20px);
}

@keyframes loader {
  0% {
    left: -100px
  }
  100% {
    left: 110%;
  }
}

#box {
  width: 50px;
  height: 50px;
  background: #1d80e1;
  animation: animate .5s linear infinite;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 5px;
  width: 80px;
  height: 80px;
  background-size: 50px;
  background-position: center;
  background-image: url("https://picsum.photos/id/10/80/80");
  background-repeat: no-repeat;
}

@keyframes animate {
  17% {
    border-bottom-right-radius: 3px;
  }
  25% {
    transform: translateY(9px) rotate(22.5deg);
  }
  50% {
    transform: translateY(18px) scale(1, .9) rotate(45deg);
    border-bottom-right-radius: 40px;
  }
  75% {
    transform: translateY(9px) rotate(67.5deg);
  }
  100% {
    transform: translateY(0) rotate(90deg);
  }
}

#shadow {
  width: 50px;
  height: 5px;
  background: #000;
  opacity: 0.1;
  position: absolute;
  top: 59px;
  left: 0;
  border-radius: 50%;
  animation: shadow .5s linear infinite;
}

@keyframes shadow {
  50% {
    transform: scale(1.2, 1);
  }
}

body {
  background: #e4e4e4;
  overflow: hidden;
}

h4 {
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  font-weight: 200;
  opacity: .5;
  font-family: sans-serif;
  color: #fff;
}
<div id="loader">
  <div id="shadow"></div>
  <div id="box"></div>
</div>

1 个答案:

答案 0 :(得分:1)

在这种情况下,我使用了反向流。您可以自定义animate2。 animate2 .5s infinite linear reverse;

#loader {
  /* Uncomment this to make it run! */
  /*
     animation: loader 5s linear infinite; 
  */
  position: absolute;
  top: calc(50% - 20px);
  left: calc(50% - 20px);
}

@keyframes loader {
  0% {
    left: -100px
  }
  100% {
    left: 110%;
  }
}

#box:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 5px;
  width: 80px;
  height: 80px;
  background-size: 50px;
  background-position: center;
  background-image: url("https://picsum.photos/id/10/80/80");
  background-repeat: no-repeat;
  animation: animate2 .5s infinite linear reverse;
}

#box {
  animation: animate .5s infinite linear;
  width: 50px;
  height: 50px;
  background: #1d80e1;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 5px;
  width: 80px;
  height: 80px;
}


@keyframes animate {
  17% {
    border-bottom-right-radius: 3px;
  }
  25% {
    transform: translateY(9px) rotate(22.5deg);
  }
  50% {
    transform: translateY(18px) scale(1, .9) rotate(45deg);
    border-bottom-right-radius: 40px;
  }
  75% {
    transform: translateY(9px) rotate(67.5deg);
  }
  100% {
    transform: translateY(0) rotate(90deg);
  }
}

@keyframes animate2 {
  17% {
  }
  25% {
    transform:rotate(22.5deg);
  }
  50% {
    transform:rotate(45deg);
  }
  75% {
    transform:rotate(67.5deg);
  }
  100% {
    transform:rotate(90deg);
  }
}

#shadow {
  width: 50px;
  height: 5px;
  background: #000;
  opacity: 0.1;
  position: absolute;
  top: 59px;
  left: 0;
  border-radius: 50%;
  animation: shadow .5s linear infinite;
}

@keyframes shadow {
  50% {
    transform: scale(1.2, 1);
  }
}

body {
  background: #e4e4e4;
  overflow: hidden;
}

h4 {
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  font-weight: 200;
  opacity: .5;
  font-family: sans-serif;
  color: #fff;
}
<div id="loader">
  <div id="shadow"></div>
  <div id="box"></div>
</div>