Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  87] [ 6]  / answers: 1 / hits: 77335  / 12 Years ago, mon, may 21, 2012, 12:00:00

I need to make a FadeOut method (similar to jQuery) using D3.js. What I need to do is to set the opacity to 0 using transition().



d3.select(#myid).transition().style(opacity, 0);


The problem is that I need a callback to realize when the transition has finished. How can I implement a callback?


More From » d3.js

 Answers
11

You want to listen for the end event of the transition.



// d3 v5
d3.select(#myid).transition().style(opacity,0).on(end, myCallback);

// old way
d3.select(#myid).transition().style(opacity,0).each(end, myCallback);



  • This demo uses the end event to chain many transitions in order.

  • The donut example that ships with D3 also uses this to chain together multiple transitions.

  • Here's my own demo that changes the style of elements at the start and end of the transition.



From the documentation for transition.each([type],listener):




If type is specified, adds a listener for transition events, supporting both start and end events. The listener will be invoked for each individual element in the transition, even if the transition has a constant delay and duration. The start event can be used to trigger an instantaneous change as each element starts to transition. The end event can be used to initiate multi-stage transitions by selecting the current element, this, and deriving a new transition. Any transitions created during the end event will inherit the current transition ID, and thus will not override a newer transition that was previously scheduled.




See this forum thread on the topic for more details.



Finally, note that if you just want to remove the elements after they have faded out (after the transition has finished), you can use transition.remove().


[#85439] Sunday, May 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;