How to use Snap.svg code multiple times?

我与影子孤独终老i 提交于 2019-12-12 05:38:03

问题


this demo .

I want to make Icons using Snap.svg, so i want end user to repeating HTML to getting multiple icons.

HTML:

<svg class="box"></svg>
<svg class="box"></svg>    <!-- not work -->
<svg class="box"></svg>    <!-- not work -->
<svg class="box"></svg>    <!-- not work -->

JS:

var box = Snap(".box");
box.rect(0,0,100,100).attr({fill:f00});

回答1:


You need to iterate through each .box and instantiate Snap on each element, this is an example:

var boxes = [].slice.call(document.querySelectorAll(".box"));
boxes.forEach(function(box){
  var box = Snap(box);
  box.rect(0,0,100,100).attr({fill:"#f00"});
});

Working example here:

http://codepen.io/anon/pen/XKXpEY



来源:https://stackoverflow.com/questions/37731422/how-to-use-snap-svg-code-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!