首页 > 解决方案 > CSS @keyframe 动画删除整个按钮,而不仅仅是边框

问题描述

我有一个勾勒按钮边框的 CSS 关键帧动画。但是,当我尝试在鼠标关闭时反转动画时,它会删除插入框阴影而不是渐变边框。

正如您在下面看到的,我尝试在mouseLeaveAnimationClass. 这也不能解决问题。这个问题有优雅的解决方案吗?

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');
  });
}
 body {

   background-color: 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: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;
}

.get-started.mouseleaveAnimationClass {
  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;
}


/* 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%);
  }
}
<div class="get-started" id="button">Get Started</div>

可运行示例

标签: javascripthtmlcssanimationkeyframe

解决方案


在您最初的问题中,该.get-started.mouseleaveAnimationClass {规则适用于元素本身,而不是::after伪元素,这就是元素被裁剪的原因。但是,这不会解决您的主要问题 - 反向动画。

我已经使用不是超级 DRY 的解决方案更新了您的代码,您可能可以改进它。

仅在第一次悬停时,.ready该类被添加到按钮中。这将启用frame-leave动画,而无需运行它。每当您应用动画:hover元素frame-enter时,一旦您离开元素,frame-leave就会再次调用 。

笔记:

  1. frame-enter并且frame-leave是相同的动画。使用不同的名称可以让我们替换它们。
  2. 当您在动画中间进入然后离开时,它将从进入动画跳转到离开动画。

var els = document.querySelectorAll('.get-started');
for (var i = 0; i < els.length; i++) {
  els[i].addEventListener('mouseenter', function(e) {
    e.target.classList.add('ready');
  }, { once: true });
}
#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::after {
  content: '';
  position: absolute;
  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%;
  clip-path: polygon(0% 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 0% 100%);
}

.get-started.ready::after {
  animation: frame-leave 1s forwards ease-in-out, gradient-animation 4s ease-in-out infinite;
}

.get-started.ready:hover::after {
  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% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 0%, 0% 0%);
  }
  100% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 0% 100%);
  }
}

@keyframes frame-leave {
  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% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 5px, 5px 0%, 0% 0%);
  }
  100% {
    clip-path: polygon(0% 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 5px 100%, 0% 100%);
  }
}
<div class="get-started" id="button">Get Started</div>


推荐阅读