0

I have a single <div> on my page. red square and I want 2 animations to be applied to it:

  1. Move this square on page load
  2. Change the colour of the square on mouse hover

Here is the code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
    .animation {
        width: 150px;
        height: 150px;
        background-color: red;
        animation-name: anim;
        animation-duration: 3s;
        animation-fill-mode: forwards;
    }

    .animation:hover {
        animation-name: hover;
        animation-duration: 1s;

    }

    @keyframes anim {
        from {
        }
        30% {
            transform: translateX(100px);
        }
        70% {
            transform: translate(100px, 100%);
            /*background-color: blue;*/
        }
        100% {
            transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
        }
    }

    @keyframes hover {
        from {
        }
        to {
            background:yellow;
        }
    }
</style>
</head>
<body>
<div class="animation">
</div>
</body>
</html>

https://jsfiddle.net/tbqj4goy/

The problem with it is that when I hover the mouth the first annimation interrupts and starts from the beginning.

Any ideas how to let these annimation collaborate without problems?

3
  • 1
    no need a second animation, use a simple transition for the color change Commented Jun 19, 2019 at 9:29
  • @TemaniAfif I tried this, but I'm just curious is it possible to use multiple annotations, and why if not Commented Jun 19, 2019 at 9:31
  • 1
    you can and you need to put them in the same animation. Example: stackoverflow.com/a/56510103/8620333 Commented Jun 19, 2019 at 9:33

2 Answers 2

3

.animation {
    width: 150px;
    height: 150px;
    background-color: red;
    animation-name: anim;
    animation-duration: 3s;
    animation-fill-mode: forwards;
    transition: 1s;
}

.animation:hover {
    background-color: yellow;
    transition: 1s;
}

@keyframes anim {
    from {}

    30% {
        transform: translateX(100px);
    }

    70% {
        transform: translate(100px, 100%);
        /*background-color: blue;*/
    }

    100% {
        transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="animation">
</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

one more question regarding your solution: if I'm moving mouth off the square it immediately becomes red. is it possible to make it animated as well(yellow -> red on mouth off)?
Yeah ofcourse, I editted it. You just have to add the transition to the original class (without any state)
0

For change background color you don't need animation, just add background property to :hover;

        .animation {
            width: 150px;
            height: 150px;
            background-color: red;
            transition: background-color 250ms ease;
            animation: anim 3s forwards;
        }

        .animation:hover {
           background-color: yellow;
        }

        @keyframes anim {
            from {
            }
            30% {
                transform: translateX(100px);
            }
            70% {
                transform: translate(100px, 100%);
                /*background-color: blue;*/
            }
            100% {
                transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
            }
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="animation">
</div>
</body>
</html>

2 Comments

You posted same solution as me later but forgot the transition of the hover.
First of all i didn't see your comment when I wrote my comment and adding transition only element is enough. You can check code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.