首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >悬停上的CSS逆向变形动画

悬停上的CSS逆向变形动画
EN

Stack Overflow用户
提问于 2022-03-22 13:26:33
回答 1查看 76关注 0票数 0

我的页面上有圆圈元素动画,我想要做的是在悬停时平稳地转换它,使它与原来的大小成正方形:200 px200 my。并且,鼠标离开顺利回到以前的(变形)状态。有什么想法吗?这是我的代码:

代码语言:javascript
复制
a {
  width: 200px;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  color: #fff;
  border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  animation: morphing ease-in-out 10s infinite;
}

a:hover {

}

@keyframes morphing {
  0% {
    border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  }
  25% {
    border-radius: 58% 42% 75% 25% / 76% 46% 54% 24%;
  }
  50% {
    border-radius: 50% 50% 33% 67% / 55% 27% 73% 45%;
  }
  75% {
    border-radius: 33% 67% 58% 42% / 63% 68% 32% 37%;
  }
}
代码语言:javascript
复制
<a>sample text</a>

EN

回答 1

Stack Overflow用户

发布于 2022-03-22 14:57:38

好了,就这样吧。在mouseover上,我们启动变形动画。在mouseout上,我们将边界半径设置为起始位置。

您可以根据您的喜好使用持续时间(以毫秒为单位)。

代码语言:javascript
复制
let elem = document.querySelector("a");
let morphAnim = elem.getAnimations()[0];

elem.addEventListener("mouseover", () => {
  elem.animate(morphAnim.effect.getKeyframes(), {
    easing: "ease-in-out",
    duration: 10000,
    iterations: Infinity,
    iterationStart: 0.1
  })
})


elem.addEventListener("mouseout", () => {
  elem.animate({
    borderRadius: "30% 70% 70% 30% / 30% 30% 70% 70%"
  }, {
    duration: 1000,
    fill: "forwards",
    iterations: 1
  });

})
代码语言:javascript
复制
a {
  width: 200px;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  color: #fff;
  border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  animation: morphing ease-in-out 10s infinite;
  animation-play-state: paused;
}

@keyframes morphing {
  0% {
    border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  }
  25% {
    border-radius: 58% 42% 75% 25% / 76% 46% 54% 24%;
  }
  50% {
    border-radius: 50% 50% 33% 67% / 55% 27% 73% 45%;
  }
  75% {
    border-radius: 33% 67% 58% 42% / 63% 68% 32% 37%;
  }
}
代码语言:javascript
复制
<a>sample text</a>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71572866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档