Tom*_*nes 4 html text transition d3.js
我正在尝试在D3中做一些简单的文本框,并在SO上使用另一个q&a我已经发现我需要使用foreignObject并将文本包装在div中.那一切都很好.我想要做的是通过点击其他东西来更新文本.我可以更新文本本身,但不能更新它的大小或颜色.这不可能是正确的.这是我的代码.
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//add some text for clicking
svg.append("text")
.attr("x", "260")
.attr("y", "40")
.attr("font-family","sans-serif")
.attr("font-size", "18")
.attr("fill", "black")
.attr("id", "clickone")
.text("Click this")
;
//this is the foreignObject with the original text
svg.append('foreignObject')
.attr('x', 40)
.attr('y', 100)
.attr('width', 180)
.attr('height', 100)
.append("xhtml:body")
.attr("id","words")
.html('<div style="width: 160px;"><p>Old text old text old text old text old text old text<p></div>');
//and here's the click and transition
svg.select("svg #clickone")
.on("click", function() {
svg.select('p')
.transition()
.delay(500)
.text("new text new text new text new text new text")
.attr("fill","red")
.attr("font-size","20")
})
Run Code Online (Sandbox Code Playgroud)
;
因此,在该示例中,文本更新,转换延迟,但颜色和大小不会更改.没有CSS或任何东西,只有代码.我究竟做错了什么?
该<p>标签不具有fill或font-size属性.它确实有一个css样式属性,你可以使用它来做或多或少相同的事情,你可以改变它transition.style
这看起来像这样:
svg.select("svg #clickone").on("click", function() {
svg.select('p')
.transition()
.duration(2500)
.text("new text new text new text new text new text")
.style("background-color","red")
.style("font-size","20px")
});
Run Code Online (Sandbox Code Playgroud)