单击后删除SVG元素

时间:2016-12-11 08:01:19

标签: javascript jquery html svg raphael

因此,对于我的作业,我有一个网页,我输入一个数字并选择一个形状,所选形状的选定数量将出现并通过一组动画。在动画之后,形状将消失,但在此之前,如果单击它,形状也应该消失。我试过使用remove()函数,但无法正确使用。请帮忙。

这是我的javascript:

draw = function() {
  var typed = $('#howmany').val()
  var shape = $('#shape').val()
  var SVG = $("svg");
  var x, y;

  for (var i = 0; i < typed; i++) {
    x = Math.random() * 350
    y = Math.random() * 350
    if (shape == 'a') {
      pattern = paper.circle(25, 25, 25)
    }
    if (shape == 'b') {
      pattern = paper.rect(10, 10, 50, 50)
    }
    if (shape == 'c') {
      pattern = paper.path('M25,0 L50,50, L0,50 Z')
    }

    color_attr = {
        'fill': '#BB7'
    }

    position_attr = {
      'transform': 't' + x + ',' + y
    }

    pattern.attr(color_attr)
    pattern.animate(position_attr, 2000)
        pattern.click(remove())
        setTimeout(function(){
      SVG.find("circle").remove();
      SVG.find("rect").remove();
      SVG.find("path").remove();
    }, 2000);
  }
}

setup = function() {
  paper = Raphael('svg1', 400, 400)
  $('button').click(draw)
}
jQuery(document).ready(setup)

这是小提琴:https://jsfiddle.net/o6e2yu5b/3/

3 个答案:

答案 0 :(得分:2)

您需要将click事件绑定到模式并在单击时将其删除。根据您的代码,您需要使用IIFE附加事件处理程序,因此您不会遇到关闭问题。

这是你能做的。

(function(currentPattern) {
  currentPattern.node.onclick = function(){
    currentPattern.remove();
  };
})(pattern);

以下是更新后的jsFiddle

答案 1 :(得分:0)

我不知道您是否需要删除所有形状或一个特定的点击形状。如果你想删除所有,只需在setTimeout函数之前添加此函数:

    $('body').click(function(e){
    var element = e.target.tagName;
    if (element === 'circle')
{    SVG.find(element).remove(); }
});

如果要删除特定的一个,请尝试通过ID识别每个圆圈,然后创建一个if语句来删除具有所单击元素ID的形状。

答案 2 :(得分:0)

我猜你需要在你的JS片段中进行一次小整改,然后才能正常工作。只需用以下代码替换删除代码:

  

pattern.click(pattern.remove)

draw = function() {
  var typed = $('#howmany').val()
  var shape = $('#shape').val()
  var SVG = $("svg");
  var x, y;

  for (var i = 0; i < typed; i++) {
    x = Math.random() * 350
    y = Math.random() * 350
    if (shape == 'a') {
      pattern = paper.circle(25, 25, 25)
    }
    if (shape == 'b') {
      pattern = paper.rect(10, 10, 50, 50)
    }
    if (shape == 'c') {
      pattern = paper.path('M25,0 L50,50, L0,50 Z')
    }
    
    color_attr = {
    	'fill': '#BB7'
    }

    position_attr = {
      'transform': 't' + x + ',' + y
    }
		
    pattern.attr(color_attr)
    pattern.animate(position_attr, 2000)
    pattern.click(pattern.remove)
		setTimeout(function(){
      SVG.find("circle").remove();
      SVG.find("rect").remove();
      SVG.find("path").remove();
    }, 2000);
  }
}

setup = function() {
  paper = Raphael('svg1', 400, 400)
  $('button').click(draw)
}
jQuery(document).ready(setup)
body {
  max-width: 40em;
  line-height: 1.6;
  margin: 0 auto;
  padding: 0.5em;
  color: black;
  font-family: "Helvetica", "Arial", sans-serif;
}

h1,
h2,
h3 {
  line-height: 1.2;
  color: black;
}

@media print {
  body {
    line-height: 1.4;
  }
}

svg {
  border: thin solid black;
}

input {
  width: 2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.7/raphael.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>


<main>

  <h1>Assignment 4 : Zap-em</h1>

  <p>Difficulty:
    <input type="text" id="howmany" />
  </p>

  <p>
    Shape:
    <select id="shape">
      <option value="a">Circle</option>
      <option value="b">Square</option>
      <option value="c">Triangle</option>
    </select>
  </p>
  <button id="btn">Start</button>

  <div id="svg1"></div>



</main>

更新了小提琴here。 详细了解clickremove