Staggered? Chained? Single DOM element addressed by a series of d3 transitions of varying delay() and duration()

霸气de小男生 提交于 2019-12-06 04:53:34

You have basically two options here. First, you can use the normal chained transition pattern, computing the delays of subsequent transitions based on the duration of the previous transitions:

 d3.select("svg").append("circle")
   .attr("transform", "translate(20,20)")
   .attr("r", 20)
   .attr("fill", "black")
   .transition().duration(1000)
   .attr("fill", "orange")
   .transition().delay(1500).duration(500)
   .attr("fill", "blue");

The delay of the second transition, 1500, is the duration of the first transition, 1000, plus the delay before the second transition starts, 500. If you have access to those numbers (e.g. from the data bound to your elements), you should be able to compute the delays for subsequent transitions quite easily.

The alternative is to use transition.each() to attach a handler for the "end" event and use that to set up the second transition:

d3.select("svg").append("circle")
  .attr("transform", "translate(100,20)")
  .attr("r", 20)
  .attr("fill", "black")
  .transition().duration(1000)
  .attr("fill", "orange")
  .each("end", function() {
    d3.select(this)
    .transition().delay(500).duration(500)
    .attr("fill", "blue");
  });

Here the delay of the second transition is relative to the first one, i.e. only starts when the first transition is finished. This approach does however require you to have these nested calls, which may or may not be feasible depending on what you're looking for.

If you can start transitions without delay, all works as expected:

d3.select("svg").append("circle")
  .attr("transform", "translate(180,20)")
  .attr("r", 20)
  .attr("fill", "black")
  .transition().duration(1000)
  .attr("fill", "orange")
  .transition().duration(1000)
  .attr("fill", "blue");

Here the second transition starts when the first one is finished.

Complete demo here.

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