[英]Looping transition in D3 version 4 and 5
The example answer which was given here works fine with D3 version 3, but in version 4/5 .each
was changed to .on
and the example doesn't work anymore, even if changing .each
to .on
. 此处给出的示例答案在 D3版本3上可以正常使用,但在版本4/5中,
.each
已更改为.on
,即使将.each
更改为.on
,示例也不再.on
。 Is there anything else which have to be considered? 还有什么需要考虑的吗? Here is the fiddle and the code with D3 version 4: jsfiddle
这是D3版本4的小提琴和代码: jsfiddle
var svg = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 500);
// code, code, code, irrelevant code...
function timeForTimeline(){ // har
var timeline = svg.append("line")
.attr("stroke", "steelblue");
repeat();
function repeat() {
timeline.attr({
'x1': 0,
'y1': 130,
'x2': 168,
'y2': 130
})
.transition()
.duration(4000)
.ease("linear")
.attr({
'x1': 0,
'y1': 430,
'x2': 168,
'y2': 430
})
.each("end", repeat);
};
};
timeForTimeline();
d3v4 moved away from using object literals (ie {}
) to set attributes (and classes and styles). d3v4不再使用对象文字(即
{}
)来设置属性(以及类和样式)。 You can add support back for setting attributes and styles by including d3-selection-multi ( https://d3js.org/d3-selection-multi.v1.min.js ), and calling .attrs
instead of .attr
. 您可以通过添加d3-selection-multi( https://d3js.org/d3-selection-multi.v1.min.js )并调用
.attrs
而不是.attr
来增加对设置属性和样式的支持。 Alternatively you can just use multiple .attr
. 或者,您可以只使用多个
.attr
。
Also, .ease()
now takes an easing function instead of a string. 此外,
.ease()
现在采用了缓动函数而不是字符串。 For convenience, d3v4 includes a slew of easing functions (viewable at https://github.com/d3/d3-ease ). 为了方便起见,d3v4包含了一系列缓动功能(可在https://github.com/d3/d3-ease上查看)。 You'd invoke it via something like selection .
您可以通过诸如selection之类的方法来调用它。 ease (
d3.easeLinear
). 缓解 (
d3.easeLinear
)。
Updated Fiddle: http://jsfiddle.net/wqptuews/ 更新的小提琴: http : //jsfiddle.net/wqptuews/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.