I am trying to change the direction of a moving div which is animated in css using keyframes and i want to be able to click on it and change its direction. here is what my code looks like.
HTML
<div id ="div1"></div>
CSS
#div1 {
height:50px;
width:50px;
background:red;
position:relative;
animation: oldanimation 5s infinite alternate;
}
@keyframes newanimation {
0% {
top:0px;
left:0px;
}
100% {
top:300px;
right:300px;
}
}
@keyframes oldanimation{
0% {
top:0px;
left:0px;
transform:rotate(45deg);
}
100% {
top:100px;
left:400px;
transform:rotate(-45deg);
}
}
Javascript
div1 = document.getElementById('div1');
div1.addEventListener('click', newfunc);
function newfunc () {
this.style.animationName = 'newanimation';
}
The square div currently moves from left to right and goes slightly downwards and is triggered by keyframes 'oldanimation' by default. So I decided to create a new keyframes animation labeled 'newanimation' and is trigerred when the square is clicked on. I want the square to make a smooth transition from the old path to the new path. currently it just disappears and follows the new path. is there a way to make the transition smooth? sorry for the long question. thank you.