d3.js transition end event

前端 未结 3 735
情歌与酒
情歌与酒 2020-12-03 21:32

I am applying a transition to a group of nodes returned by selectAll(). I thought the end event would fire after all transitions finished, but each(\"end\

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 22:17

    Here's a clean way to accomplish what you want:

    function endAll (transition, callback) {
        var n;
    
        if (transition.empty()) {
            callback();
        }
        else {
            n = transition.size();
            transition.each("end", function () {
                n--;
                if (n === 0) {
                    callback();
                }
            });
        }
    }
    

    You can then easily call this function like so:

    selection.transition()
        .call(endAll, function () {
            console.log("All the transitions have ended!");
        });
    

    This will work even if the transition is empty.

提交回复
热议问题