css - SVG 动画 - 来自中心的 SVG 的 CSS 悬停动画

标签 css animation svg hover

我在GSAP中做了这个效果,这里是Codepen作为引用:

https://codepen.io/whitelionx/full/vYGQqBZ

const svgs = document.querySelectorAll("svg");

svgs.forEach((svg) => {
  const tl = gsap
    .timeline({
      defaults: { ease: "power1.in" },
      paused: true
    })
    .to(svg.children[0], { drawSVG: "50% 50%" })
    .from(svg.children[1], { drawSVG: "0% 0%" }, 0);

  svg.addEventListener("mouseenter", () => tl.play());
  svg.addEventListener("mouseleave", () => tl.reverse());
});

现在我想仅使用 CSS 来完成此操作,以便当我将鼠标悬停在 svg 上时得到相同的效果,这是我的代码沙箱:

https://codesandbox.io/s/xenodochial-benz-17lss?file=/src/styles.css

最佳答案

我已经修改了一些内容来为描边虚线数组设置动画。

body {
    background: #000;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    flex-direction: column;
}

svg {
    width: 50px;
    height: 50px;
    margin: 25px;
}

.circle {
    stroke-dasharray: 28.3,0,28.3;
    transform-origin: 50% 50%;
    transform: rotate(180deg);
    transition: stroke-dasharray 0.5s linear;
}

.line {
    stroke-dasharray: 20;
    stroke-dashoffset: 20;
    transition: stroke-dashoffset 0.5s linear;
}

svg:hover .circle {
    stroke-dasharray: 0,56.0;
}

svg:hover .line {
    stroke-dashoffset: 0;
}
<svg
  version="1.1"
  shape-rendering="auto"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  viewBox="0 0 20 20"
  xml:space="preserve">
 <path class="circle" fill="none" stroke="#FFFFFF" stroke-miterlimit="10" d="M10,1c5,0,9,4,9,9s-4,9-9,9s-9-4-9-9S5,1,10,1z"/>
 <path class="line" fill="none" stroke="#FFFFFF" stroke-miterlimit="10" d="M10,0v20"/>
</svg>

关于css - SVG 动画 - 来自中心的 SVG 的 CSS 悬停动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64425450/

相关文章:

css - 在没有javascript的情况下重启CSS3动画?

c# - 基于网格可见性的 WPF 动画

jquery - 将 D3.js CSS 样式转换为内联样式

image - IE9 - 11 SVG 背景模糊

iphone - UIView 动画第一次不起作用

css - SVG stroke-dashoffset 不适用于 safari

Jquery 工具提示动画

javascript - 删除 .focusout 上的 css 类

javascript - 一次展开/折叠一个

相当于 :has() 的 Css