Animation to move image inside a span

Asked

Viewed 319 times

2

I have a div with the id="origem" which contains an image of a star, how can I move the image to the span with the id="destino" using an animation of for example 2 seconds, the path has to be in a straight line or can make a curve? It can also be in a loop as in the image?

inserir a descrição da imagem aqui

img {
    width: 2rem;
    height: 2rem;
}
<body>

    <div>fazer imagem ficar na direita desta frase: 
        <span id="destino"> 
                
        </span>
    </div>

    <br><br><br><br><br><br><br>
    
    <div id="origem">------------------------------------------------------------------------
        <img src="https://image.flaticon.com/icons/svg/148/148841.svg" alt="">
    </div>

</body>

1 answer

3


Guy with the link quoted by Anderson you get something much more sophisticated, but only with CSS and some tricks you get something similar, without SVG, and even so, "bending" the line of animation.

inserir a descrição da imagem aqui

The detail is that while the animation happens you will decrease the width container at the same time it changes the background position of bottom for top, and with that you will create a curve.

inserir a descrição da imagem aqui

The idea is to use a pseudo-element in the container, and not two elements "totally" separated as was your idea. This will greatly facilitate the idea of animation which is now a simple @keyframes

Follow the code of the animation

.origem::after {
	content: "";
	position: absolute;
	display: inline-block;
	height: 200px;
	width: 200px;
	background-image: url(https://image.flaticon.com/icons/svg/148/148841.svg);
	background-size: 20px 20px;
	background-position: bottom right;
	background-repeat: no-repeat;

	animation: star 2s linear infinite;
}

@keyframes star {
	75%, 100% {
		height: 20px;
		width: 20px;
		background-position: top right;
	}
}
<div class="origem">
	Lorem ipsum dolor sit amet.
</div>
<div class="origem">
	Lorem ipsum dolor sit amet. Lorem, ipsum dolor.
</div>

  • Thanks, helped a lot to realize how the animations works, the link to your other question/ answer was also very useful, I will give a studied in the other solution, I did not want the animation repeated changed infinite for forwards, for now seems to be to my liking.

  • 1

    @Nice Vik that worked there, the other with SVG is a little more complex, but tb is possible to be done. But this model I left here I believe is the best for your particular case... Yes forwards to animation at the end, if you want the animation to stay straight just remove bg-position. []s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.