【发布时间】:2019-09-16 22:33:20
【问题描述】:
请看下面的代码。我一生都无法弄清楚阻碍onclick 事件的JavaScript 出了什么问题。当然,CSS 样式运行良好,但即使 JS 在 codepen 上按原样运行,它也不会在这里运行。
<svg id="mySVG" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
#mySVG {
cursor: pointer;
}
@keyframes circleGrow {
to {r:50}
}
#myCircle {
animation: circleGrow 1s 1s forwards;
}
@keyframes strokeDraw {
to {stroke-dashoffset: 0}
}
@keyframes toFill {
to {fill: white;}
}
#myText {
animation: strokeDraw 2.5s 2s linear forwards,
toFill 1s 4.5s linear forwards;
}
</style>
<script>
//<![CDATA[
var mySVG = document.getElementById("mySVG"),
myCircle = document.getElementById("myCircle"),
myText = document.getElementById("myText");
mySVG.onclick = function(){
myCircle.style.animation = "null";
myText.style.animation = "null";
setTimeout(function(){
myCircle.style.animation = "";
myText.style.animation = "";
},10);
}
//]]>
</script>
<circle id="myCircle" cx="50" cy="50" r="0" fill="maroon" />
<text id="myText" x="50%" y="50%" text-anchor="middle" alignment-baseline="central" font-size="40" fill="transparent" stroke="white" stroke-dasharray="190" stroke-dashoffset="190">Hello</text>
</svg>
【问题讨论】:
标签: javascript animation svg cdata embedding