可以将CSS3动画文本阴影样式应用于SVG吗?

时间:2019-02-14 16:36:28

标签: css css3

我找到了这个cool neon CSS effect,我希望能够将其应用于SVG。不幸的是,它使用的是text-shadow,所以我想知道是否有一种方法可以将相同的样式应用于我的SVG。我尝试仅从text-shadow更改为box-shadow,但这仅适用于SVG的外部边界。

body {
  background-color: #222222;
  background: repeating-linear-gradient(45deg, #2b2b2b 0%, #2b2b2b 10%, #222222 0%, #222222 50%) 0 / 15px 15px;
}

h2 {
  font-family:sans-serif;
  font-size:30px;
  color: #FFDD1B;
  -webkit-animation: neon 1.5s ease-in-out infinite alternate;
  animation: neon 1.5s ease-in-out infinite alternate;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}

img {
  width:150px;
  -webkit-animation: neonSVG 1.5s ease-in-out infinite alternate;
  animation: neonSVG 1.5s ease-in-out infinite alternate;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}

body {
  background-color: #222222;
  background: repeating-linear-gradient(45deg, #2b2b2b 0%, #2b2b2b 10%, #222222 0%, #222222 50%) 0 / 15px 15px;
}

h2 {
  font-family:sans-serif;
  font-size:30px;
  color: #FFDD1B;
}

img {
  width:150px;
}

@keyframes neon {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #FFDD1B, 0 0 70px #FFDD1B, 0 0 80px #FFDD1B, 0 0 100px #FFDD1B, 0 0 150px #FFDD1B;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #FFDD1B, 0 0 35px #FFDD1B, 0 0 40px #FFDD1B, 0 0 50px #FFDD1B, 0 0 75px #FFDD1B;
  }
}
@-webkit-keyframes neon {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #FFDD1B, 0 0 70px #FFDD1B, 0 0 80px #FFDD1B, 0 0 100px #FFDD1B, 0 0 150px #FFDD1B;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #FFDD1B, 0 0 35px #FFDD1B, 0 0 40px #FFDD1B, 0 0 50px #FFDD1B, 0 0 75px #FFDD1B;
  }
}

@keyframes neonSVG {
  from {
    box-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #FFDD1B, 0 0 70px #FFDD1B, 0 0 80px #FFDD1B, 0 0 100px #FFDD1B, 0 0 150px #FFDD1B;
  }
  to {
    box-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #FFDD1B, 0 0 35px #FFDD1B, 0 0 40px #FFDD1B, 0 0 50px #FFDD1B, 0 0 75px #FFDD1B;
  }
}
@-webkit-keyframes neonSVG {
  from {
    box-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #FFDD1B, 0 0 70px #FFDD1B, 0 0 80px #FFDD1B, 0 0 100px #FFDD1B, 0 0 150px #FFDD1B;
  }
  to {
    box-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #FFDD1B, 0 0 35px #FFDD1B, 0 0 40px #FFDD1B, 0 0 50px #FFDD1B, 0 0 75px #FFDD1B;
  }
}
<h2>This is some text</h2>

<img src="https://camo.githubusercontent.com/133ab8b9b686378b2f2c70b215b369ec4d24c8a9/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f616479656e2e737667" />

1 个答案:

答案 0 :(得分:2)

您需要使用filter使其与SVG一起使用。然后用普通的CSS过渡或@keyframes为它设置动画。

这是一个悬停动画的例子。

svg {
  filter: drop-shadow(12px 12px 7px rgba(0,0,0,0.5));
  transition: all 0.25s ease-in-out;
}

svg:hover {
  filter: drop-shadow(5px 5px 3px rgba(67,237,82,0.9));
}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="180px" height="49px" viewBox="0 0 180 49" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g id="svgText" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" font-family="Lato-Black, Lato" font-size="40" font-weight="700" letter-spacing="-0.333333343">
        <text id="SVG-Text" fill="#A5A5A7">
            <tspan x="8" y="40">SVG Text</tspan>
        </text>
    </g>
</svg>