简体   繁体   English

在 d3 中转换后删除 svg 元素

[英]Remove a svg element after transition in d3

I want to show a line and make it disappear after a few seconds.我想显示一条线并让它在几秒钟后消失。 I am accomplishing this by drawing a line first and then drawing a white (same as background) after my transition.我通过先画一条线,然后在过渡后画一个白色(与背景相同)来实现这一点。 However, this is creating a problem that this creates a while line on any other SVG elements placed in that position in the meantime.但是,这会产生一个问题,即这会在同时放置在该位置的任何其他 SVG 元素上创建一个 while 行。

Is there some way to remove the line I placed after my transition time?有什么方法可以删除我在过渡时间后放置的线吗?

$svg_area
.append("line")
.attr("x1",$pair[0][0])
.attr("y1",$pair[0][1])
.attr("x2",$pair[1][0])
.attr("y2", $pair[1][1])
.attr("style","stroke:rgb(255,192,203);stroke-width:1")
.transition()
.duration(2000)
.attr("style","stroke:rgb(255,255,255)");

You don't need a transition for this您不需要为此进行过渡

 var $svgArea = d3.select('svg') var myLine = $svgArea.append("line") .attr("x1", 0) .attr("y1", 150) .attr("x2", 300) .attr("y2", 150) .attr("style", "stroke:rgb(255,192,203);stroke-width:1") window.setTimeout(function() { myLine.remove() }, 2000)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <svg width=300 height=300> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM