admin管理员组文章数量:1023827
I want to give an object an attribute once a transition is finished. I'm simply updating an images position as follows:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'})
Once it finishes, I want to give the object tmp an attribute "moved" with the value "no". I tried:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}).end('moved', 'no')
But without success. Any tips? Thanks,
I want to give an object an attribute once a transition is finished. I'm simply updating an images position as follows:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'})
Once it finishes, I want to give the object tmp an attribute "moved" with the value "no". I tried:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}).end('moved', 'no')
But without success. Any tips? Thanks,
Share Improve this question asked Jun 17, 2012 at 22:02 mikemike 23.9k32 gold badges81 silver badges100 bronze badges4 Answers
Reset to default 5According to the documentation, you can use .each
:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}
).each('end', function() {
d3.select(this).attr('moved', 'no');
// or maybe also this.setAttribute('moved', 'no');
});
You can tell javascript to wait for a period of time and run code after using window.setTimeout. You simply have to sync both events up by using the same number of milliseconds.
window.setTimeout(function(){
//Your fake "callback" code here
}, 1000);
In response to @user1066286 (and because I cannot post ments): you shouldn't use setTimout() here! Appart from it being bad practice, you cannot guarantee that the transition will actually be pleted when the timeout stops.
From the d3 Transition docs:
Transitions have a four-phase life cycle:
The transition is scheduled. The transition starts. The transition runs. The transition ends.
Each of these four phazes are processed asynchronously, so there's no way to know how long the transition actually takes. It maight be a little slower then the user-defined duration, it might be a little faster.
@Felix Klings answer appears to be deprecated. See https://stackoverflow./a/10692220/14095529 (block below is quote from that answer).
// d3 v5
d3.select("#myid").transition().style("opacity","0").on("end", myCallback);
// old way
d3.select("#myid").transition().style("opacity","0").each("end", myCallback);
I want to give an object an attribute once a transition is finished. I'm simply updating an images position as follows:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'})
Once it finishes, I want to give the object tmp an attribute "moved" with the value "no". I tried:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}).end('moved', 'no')
But without success. Any tips? Thanks,
I want to give an object an attribute once a transition is finished. I'm simply updating an images position as follows:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'})
Once it finishes, I want to give the object tmp an attribute "moved" with the value "no". I tried:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}).end('moved', 'no')
But without success. Any tips? Thanks,
Share Improve this question asked Jun 17, 2012 at 22:02 mikemike 23.9k32 gold badges81 silver badges100 bronze badges4 Answers
Reset to default 5According to the documentation, you can use .each
:
tmp.transition().duration(1000)
.attr("transform", function(d) {return 'translate(' +
coordinates[d].x +',' +
coordinates[d].y + ')'}
).each('end', function() {
d3.select(this).attr('moved', 'no');
// or maybe also this.setAttribute('moved', 'no');
});
You can tell javascript to wait for a period of time and run code after using window.setTimeout. You simply have to sync both events up by using the same number of milliseconds.
window.setTimeout(function(){
//Your fake "callback" code here
}, 1000);
In response to @user1066286 (and because I cannot post ments): you shouldn't use setTimout() here! Appart from it being bad practice, you cannot guarantee that the transition will actually be pleted when the timeout stops.
From the d3 Transition docs:
Transitions have a four-phase life cycle:
The transition is scheduled. The transition starts. The transition runs. The transition ends.
Each of these four phazes are processed asynchronously, so there's no way to know how long the transition actually takes. It maight be a little slower then the user-defined duration, it might be a little faster.
@Felix Klings answer appears to be deprecated. See https://stackoverflow./a/10692220/14095529 (block below is quote from that answer).
// d3 v5
d3.select("#myid").transition().style("opacity","0").on("end", myCallback);
// old way
d3.select("#myid").transition().style("opacity","0").each("end", myCallback);
本文标签: javascriptChange object attribute following transition D3Stack Overflow
版权声明:本文标题:javascript - Change object attribute following transition D3 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745606338a2158749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论