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?

Codesigner
  • 578
  • 3
  • 16
evgeniy44
  • 2,862
  • 7
  • 28
  • 51

2 Answers2

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>
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
  • 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)? – evgeniy44 Jun 19 '19 at 11:42
  • 1
    Yeah ofcourse, I editted it. You just have to add the transition to the original class (without any state) – Wimanicesir Jun 19 '19 at 11:48
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>
novruzrhmv
  • 639
  • 5
  • 13