Search code examples
csscss-animationssvg-animate

Stop an animated SVG to a certain position on screen


Using this example, is there a way to stop this CSS animation to a fixed point on the screen? So for instance, it's moving across and I decide to have it stop like 20px from the top right of the screen. Is this possible with just CSS?

.bird {
  background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/174479/bird-cells.svg);
  background-size: auto 100%;
  width: 88px;
  height: 125px;
  will-change: background-position;
  animation-name: fly-cycle;
  animation-timing-function: steps(10);
  animation-iteration-count: infinite;
}
.bird--one {
  animation-duration: 1s;
  animation-delay: -0.5s;
}

.bird-container {
  position: absolute;
  top: 20%;
  left: -10%;
  transform: scale(0) translateX(-10vw);
  will-change: transform;
  animation-name: fly-right-one;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}
.bird-container--one {
  animation-duration: 15s;
  animation-delay: 0;
}

@keyframes fly-cycle {
100% {
	background-position: -900px 0;
}
}

@keyframes fly-right-one {

0% {
	transform: scale(0.3) translateX(-10vw);
}

10% {
	transform: translateY(2vh) translateX(10vw) scale(0.4);
}

20% {
	transform: translateY(0vh) translateX(30vw) scale(0.5);
}

30% {
	transform: translateY(4vh) translateX(50vw) scale(0.6);
}

40% {
	transform: translateY(2vh) translateX(70vw) scale(0.6);
}

50% {
	transform: translateY(0vh) translateX(90vw) scale(0.6);
}

60% {
	transform: translateY(0vh) translateX(110vw) scale(0.6);
}

100% {
	transform: translateY(0vh) translateX(110vw) scale(0.6);
}
}
<div style="width:100%;">
 <div class="bird-container bird-container--one">
  <div class="bird bird--one"></div>
  </div>
</div>

https://jsfiddle.net/14ndk5xg/


Solution

  • By changing the VW to a lower number, you can get it to stop a certain distance from the right side of the screen. If you always want the bird to stop when it's travelled approximately 90% of the screen width then you can change the VW to 90. With the way it's currently setup, it's not easy to make it stop at a certain amount of pixels.

    By setting your code like below at 50% and removing the higher percentages, you can get the bird to fly 90% to the right and fly up to the upper right corner.

    50% {
            transform: translateY(-20vh) translateX(90vw) scale(0.6);
        }