简体   繁体   English

触发CSS动画作为D3.js中的过渡

[英]Triggering a css animation as a transition in D3.js

I have an account balance value that I am displaying using d3. 我有一个使用d3显示的帐户余额值。 Every second, the value is updated, and depending on whether increases or decreases, I want it to flash green or red respectively. 每秒更新一次该值,然后根据它是增加还是减少,我希望它分别闪烁绿色或红色。 I have two css animations that handle the flash, and each are controlled with a modifier class that can be added to the element, like -increase or -decrease . 我有两个处理Flash的css动画,每个动画都有一个修饰符类来控制,该修饰符类可以添加到元素中,例如-increase-decrease

I am using d3's classed to control these classes, but that's where I'm running into some issues. 我使用d3的classed来控制这些类,但这就是我遇到的一些问题。 Each update loop, I check and remove both of these classes, then conditionally add the appropriate one back on to trigger a new animation. 在每个更新循环中,我都会检查并删除这两个类,然后有条件地重新添加适当的类以触发新的动画。 The problem is, if the class remains the same as the last loop, it is not being removed and added again and therefor the animation on triggers when the class changes. 问题是,如果该类与上一个循环相同,则不会将其删除并再次添加,因此,当类更改时,触发器上的动画也是如此。 How can I get one of these animations to trigger each time? 如何获得每次触发这些动画之一?

function refreshBalance(accounts){
  let sum = 0;
  for(let i=0; i < accounts.length; i++){
    sum = sum + (accounts[i].value * accounts[i].rate);
  }

  let balance = d3.select(".balance").select(".currency-value");

  let diff;
  if ((sum - balance.attr("data-balance")) > 0){
    diff = "increment";
  } else {
    diff = "decrement";
  }

  balance.attr("data-balance", normalizeCurrency(sum, 4, 4))
    .text(normalizeCurrency(sum, 4, 4));

  balance.classed("-increment", false);
  balance.classed("-decrement", false);
  balance.classed("-increment", diff === "increment");
  balance.classed("-decrement", diff === "decrement");
}

You should wrap the code that adds new classes in setTimeout function: 您应该在setTimeout函数中包装添加新类的代码:

  balance.classed("-increment", false);
  balance.classed("-decrement", false);

  setTimeout(function() {
     balance.classed("-increment", diff === "increment");
     balance.classed("-decrement", diff === "decrement");
  });

Little demo: 小样:

 var divs = d3.select('body') .data([1,2,3,4,5,6]) .enter() .append('div') .text(function(d,i) { return i; }) setInterval(function() { divs.classed("-increment", false); divs.classed("-decrement", false); setTimeout(function() { divs.classed("-increment", function(d, i) { return i % 2}); divs.classed("-decrement", function(d, i) { return !(i % 2)}); }); }, 3000); 
 div { font-size: 30px; font-weight: bold; } .-increment { animation:increment-animation 1.5s; } .-decrement { animation:decrement-animation 1.5s; } @keyframes increment-animation { 0% { color: black; } 20% { color: green; } 40% { color: black; } 60% { color: green; } 80% { color: black; } 100% { color: green; } } @keyframes decrement-animation { 0% { color: black; } 20% { color: red; } 40% { color: black; } 60% { color: red; } 80% { color: black; } 100% { color: red; } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js"></script> 

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

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