I would like to make the transition d3 to the style defined in CSS. I can successfully move on to a style value that I explicitly apply in the code. But I would like the animation target values ββto be defined in the stylesheet.
Here is an example of a red div that goes blue:
<html> <head> <script src="http://d3js.org/d3.v3.js"></script> <style> div { background: red } </style> </head> <body> <div>Hello There</div> <script> d3.select('body div') .transition() .duration(5000) .style("background", "cornflowerblue"); </script> </body> </html>
And here is my (incorrect) attempt to "save" these values ββin CSS:
<html> <head> <script src="http://d3js.org/d3.v3.js"></script> <style> div { background: red } div.myDiv { background: cornflowerblue } </style> </head> <body> <div>Hello There</div> <script> d3.select('body div') .transition() .duration(5000) .attr("class", "myDiv"); </script> </body> </html>
The former animates the background from red to blue. The latter jumps from red to blue (by the background value defined in CSS).
I think I understand why this is not working. Can I animate a CSS-style value set in d3 and how do I do this?
source share