简体   繁体   中英

Block d3 .transition() until another transition has completed

I have a hover effect implemented in d3 that selects several rectangles in an svg and changes their color:

var rect = d3.selectAll('.rect')

rect.transition()
  .duration(1000)
  .style('fill', red')

I only want this effect to be operating on one rectangle at a time. If I quickly hover over several rectangle is close succession, the effect is triggered on all the rectangles I've hovered over. How can I block other mouse hover events if there is currently another event being triggered?

You can have a global variable that acts as a semaphore:

var transitioning = false;

rect.append("...")
    .on("mouseover", hover);

function hover() {
  if(!transitioning) {
    transitioning = true;
    rect.transition()
        .duration(1000)
        .style('fill', 'red')
        .each("end", function() { transitioning = false; });
  }
}

Note that this assumes that all your transitions have the same duration and delay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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