简体   繁体   English

使用d3同时用.tween转换所有div元素

[英]Transition all div elements with .tween at the same time with d3

I am trying to modify the following example so that all of the bars begin to animate at the same time, instead of cycling through 1 by 1; 我正在尝试修改以下示例,以使所有小节都开始同时进行动画处理,而不是通过1循环显示。

Here is a plnk of complete code + snippet of d3 code I believe needs to be edited. 这是完整代码+ d3代码的片段,我认为需要对其进行编辑。

http://plnkr.co/edit/sneKYhTPAK0X8IhhExj9?p=preview http://plnkr.co/edit/sneKYhTPAK0X8IhhExj9?p=preview

function loadChart() {

    var start_val = 0;

    d3.select("body").selectAll(".pattern")
        .append("div")
        .text(start_val)
        .attr("class", "percentage")
        .transition()
        .delay(function(d, i) {
            return i * 200;
        })
        .duration(1000)
        .style("min-width", function(d, i) {
            return (d.progress * 3) / 2 + "px";
            console.log(1);
        })
        .tween(".percentage", function(d) {
            var i = d3.interpolate(this.textContent, d.progress),
                prec = (d.progress + "").split("."),
                round = (prec.length > 1) ? Math.pow(10, prec[1].length) : 1;

            return function(t) {
                this.textContent = Math.round(i(t) * round) / round + "%";
            };
        });

    d3.select("body").selectAll(".path")
        .transition()
        .delay(function(d, i) {
            return i * 200;
        })
        .duration(1000)
        .style("width", function(d, i) {
            return d.progress * 3 + "px";
        });
}

From what I can understand, .tween will cycle through each element and begin the transition. 据我了解,.tween将循环遍历每个元素并开始过渡。 I cannot find a way to enable the function to begin the transition for all elements at the same time however. 我找不到一种使函数同时开始所有元素转换的方法。

This is my first time experimenting with d3.js, so any help/advice would be much appreciated. 这是我第一次尝试d3.js,因此非常感谢您的帮助/建议。

Thanks 谢谢

Instead of this 代替这个

.delay(function(d, i) {
    return 200*i;//this will make it start at different times as i indicate the index of the element.
})

make it this: 做到这一点:

.delay(function(d, i) {
    return 200;
})

working code here 这里的工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM