CSS:悬停时启动动画,然后再也不停止

时间:2021-04-08 12:13:15

标签: html css animation hover transition

我是 CSS-Animation 的新手,所以也许对您来说答案很简单。但我还没有在这里找到答案。

我想要一个元素在悬停时开始动画,然后永不结束。这是一个艺术项目,因为它实际上没有任何意义。

我得到的是:一个小圆圈,当悬停时它开始变得越来越大。而且我希望该动画能够无休止地继续(或者像过渡一样无休止地假装它:100000 秒之类的)。

知道如何管理吗?

我得到的是这个:

.circle{
  background-color: orange;
  border-radius: 10px;
  width: 20px;
  height: 20px;
  margin-top: 10%;
  margin-left: 50%;
  transition: 1000s;
}

.circle:hover{
  transform: scale(8000);
}

.circle:hover{
  transform: scale(8000);
}
<div class="circle"></div>

非常感谢。

3 个答案:

答案 0 :(得分:4)

我将 .circle:hover 更改为一个名为“circleAnimation”的新类。然后,我给圆一个 onmouseenter 事件,将它添加到类中(因为 JavaScript 只有一行,所以我直接将其输入到 onmouseenter 事件的值中)。

结果如下:

.circle {
    background-color: orange;
    border-radius: 10px;
    width: 20px;
    height: 20px;
    margin-top: 10%;
    margin-left: 50%;
    transition: 1000s;
}

.circleAnimation {
    transform: scale(8000);
}
<div class="circle" onmouseenter="event.target.classList.add('circleAnimation')"></div>

希望这有帮助!

答案 1 :(得分:2)

我认为它可以帮助您。

.circle {
    background-color: orange;
    border-radius: 10px;
    width: 20px;
    height: 20px;
    margin-top: 10%;
    margin-left: 50%;
    transition: 1000s;
}

.animate {
    transform: scale(8000);
}
<div class="circle" onmouseenter="event.target.classList.add('animate')"></div>

您也可以使用 onmouseover 代替 onmouseenter

答案 2 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <style>
        .circle{
            background-color: orange;
            border-radius: 10px;
            width: 20px;
            height: 20px;
            margin-top: 10%;
            margin-left: 50%;
            transition: 1s;
        }
    </style>
</head>
<body>
    <div class="circle" onmouseover="changeScale(this)"></div>
    <script>
        function changeScale(circle){
            circle.style.transform = 'scale(80)';
        }
    </script>
</body>
</html>