有没有办法在点击时运行svg?

时间:2015-09-26 02:48:41

标签: javascript svg

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"width="450px" height="450px" viewBox="0 0 450 450" style="enable-background:new 0 0 450 450;" xml:space="preserve">
            <style type="text/css">
                .st0{fill:#FFFFFF;}
                .st1{fill:none;stroke:#000000;stroke-miterlimit:10;}
            </style>
            <rect id="1" x="299.8" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="2" x="150.2" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="3" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="4" x="299.8" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="5" x="150.2" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="6" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="7" x="299.8" class="st0" width="149.5" height="150.5"/>
            <rect id="8" x="150.2" class="st0" width="149.5" height="150.5"/>
            <rect id="9" class="st0" width="149.5" height="150.5"/>
            <line class="path" fill="none" stroke="#000000" x1="149.5" y1="0" x2="149.5" y2="450"/>
            <line class="path" fill="none" stroke="#000000" x1="299.5" y1="450" x2="299.5" y2="0"/>
            <line class="path" fill="none" stroke="#000000" x1="0" y1="150.5" x2="450" y2="150.5"/>
            <line class="path" fill="none" stroke="#000000" x1="450" y1="300.5" x2="0" y2="300.5"/>
        </svg>

我有这个svg,这是一个tic tac toe board。我希望它在用户输入玩家和玩家名称后加载(运行行标签的动画)。有没有办法做到这一点? rect元素是我在玩游戏时用来定位区域的东西。

.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear forwards;
}

@keyframes dash {
  to {
    stroke-dashoffset: 0;
  }
} 

1 个答案:

答案 0 :(得分:1)

就像这样或许就是点击设置课程?

function run() {
  var lines = document.getElementsByTagName("line");
  for (var i = 0; i < lines.length; i++) {
    lines[i].setAttribute("class", "path");
  }
}
line {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
}

.path {
  animation: dash 5s linear forwards;
}

@keyframes dash {
  to {
    stroke-dashoffset: 0;
  }
} 
<button onclick="run()">click me</button>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"width="450px" height="450px" viewBox="0 0 450 450" style="enable-background:new 0 0 450 450;" xml:space="preserve">
            <style type="text/css">
                .st0{fill:#FFFFFF;}
                .st1{fill:none;stroke:#000000;stroke-miterlimit:10;}
            </style>
            <rect id="1" x="299.8" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="2" x="150.2" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="3" y="300.5" class="st0" width="149.5" height="150.5"/>
            <rect id="4" x="299.8" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="5" x="150.2" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="6" y="150.5" class="st0" width="149.5" height="150.5"/>
            <rect id="7" x="299.8" class="st0" width="149.5" height="150.5"/>
            <rect id="8" x="150.2" class="st0" width="149.5" height="150.5"/>
            <rect id="9" class="st0" width="149.5" height="150.5"/>
            <line fill="none" stroke="#000000" x1="149.5" y1="0" x2="149.5" y2="450"/>
            <line fill="none" stroke="#000000" x1="299.5" y1="450" x2="299.5" y2="0"/>
            <line fill="none" stroke="#000000" x1="0" y1="150.5" x2="450" y2="150.5"/>
            <line fill="none" stroke="#000000" x1="450" y1="300.5" x2="0" y2="300.5"/>
        </svg>