javascript - 如何在鼠标关闭时反转@keyframe 动画?

标签 javascript html css animation css-animations

我需要在鼠标离开按钮时反转删除动画。有没有办法只在 CSS 中做到这一点?

我知道我可以反转动画,但我不确定将 CSS 文件放在哪里才能获得正确的动画。如果我将它放在 .get-started 中,它会删除​​边框并且我无法与按钮完全交互。

HTML:

<div class="get-started" id="button">
      <h4 class="part1">Get <span class="part2">Started</span></h4>
    </div>

CSS:

body {

  background:black;

}

#button {

    display: flex;
    font-size: 2.5rem;
    color: white;

  align-items: center;
  justify-content: center;

  width: 250px;
  height: 75px;

    position: relative;
    top: -30%;
    left: calc(50% - 125px);

}

.get-started {
  --borderWidth: 5px;
  position: relative;
  border-radius: var(--borderWidth);
    background-color: #8551FF;
    box-shadow: inset 0 0 0 5px white;
    z-index:1;
}

.get-started h4.part1 {

    color: #FFFFFF;
    font-family: 'Coves Light';
    font-weight: normal;

}

.get-started span.part2 {

    color: #5EFF00;
    font-family: 'Coves';
    font-weight: bold;

}

.get-started:after {
  content: '';
  position: absolute;
}

.get-started:hover:after {
    background: linear-gradient(60deg, #f79533, #f37055, #ef4e7b, #a166ab, #5073b8, #1098ad, #07b39b, #6fba82);
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 2px;
    background-size: 300% 300%;
    animation: frame-enter 1s forwards ease-in-out reverse, gradient-animation 4s ease-in-out infinite;
}

/* motion */
@keyframes gradient-animation {
  0% {
    background-position: 15% 0%;
  }
  50% {
    background-position: 85% 100%;
  }
  100% {
    background-position: 15% 0%;
  }
}

@keyframes frame-enter {
  0% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 5px, calc(100% - 5px) 5px, calc(100% - 5px) calc(100% - 5px), 5px calc(100% - 5px), 5px 100%, 100% 100%, 100% 0%, 0% 0%);
  }
  25% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 5px, calc(100% - 5px) 5px, calc(100% - 5px) calc(100% - 5px), calc(100% - 5px) calc(100% - 5px), calc(100% - 5px) 100%, 100% 100%, 100% 0%, 0% 0%);
  }
  50% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 5px, calc(100% - 5px) 5px, calc(100% - 5px) 5px, calc(100% - 5px) 5px, calc(100% - 5px) 5px, calc(100% - 5px) 5px, 100% 0%, 0% 0%);
  }
  75% {
    -webkit-clip-path: polygon(0% 100%, 5px 100%, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 0%, 0% 0%);
  }
  100% {
    -webkit-clip-path: polygon(0% 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 0% 100%);
  }
}

Runnable Snippet

最佳答案

你可以试试

.get-started:not(:hover):after {}

有一个反转的动画。但是它在初始页面加载时可能会有不需要的行为,我猜它总是以这个动画开始。有时你无法避免 javascript。在 mouseout 上添加一个类并在 mouseenter 上再次删除它可能会起作用,因此您可以为此类分配反向动画。

var els = document.querySelectorAll('.get-started');
for (var i = 0; i < els.length; i++) {
    els[i].addEventListener('mouseleave', function(e) {
        e.target.classList.add('mouseleaveAnimationClass');
    });
    els[i].addEventListener('mouseenter', function(e) {
        e.target.classList.remove('mouseleaveAnimationClass');
    });
}

关于javascript - 如何在鼠标关闭时反转@keyframe 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470000/

相关文章:

javascript - 将图像与链接集成

javascript - 使用 jQuery 更改父元素样式

javascript - 获取表中标题区域下方的滚动条。注意 : Table wont have thead and tbody

html - 你能在视频元素上显示 CSS 框阴影(插图)吗

css - 没有绝对定位的定位元素

javascript - 使用特殊字符编码类样式属性

javascript - 构建稀疏数组

javascript - 在 jqxGrid 中渲染 AngularJS 指令

javascript - HTML5 视频格式最佳实践

html - 没有其他元素会出现在我的带有彩色背景的部分元素中