How to achieve that bubbles are only clickable if completly zoomed d3js

风格不统一 提交于 2019-12-04 13:58:45

Let me expand a bit on @Elijah's answer:

First, I'd remove the following bit from the css:

.node--root,
.node--leaf {
  pointer-events: all;
}

in order to get more control over the pointer-events.

Next, we add some additional class to the nodes so that we can access these more easily:

.attr("class", function(d) { return 'depth' + d.depth + ' ' + 
  (d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"); })

Accordingly, every node has some additional class, like .depth4.

Now we can talk to the nodes, directly and add/remove the pointer-events as needed.

For that, and convenience, we add a new function:

function adjustPointerEvents(depth) {
  d3.selectAll('.node').attr('pointer-events', 'none');
  d3.select('.node--root').attr('pointer-events', 'all');
  d3.selectAll('.depth' + (depth + 1)).attr('pointer-events', 'all');
}

(first we remove all events, then we add the events to the node-root back (to be able to zoom out), and then we add the events for the 'next' level))

On init, we call that at level 2:

adjustPointerEvents(2);

By the way: you have some sort of an additional layer there ('Kommunikation und Umwelt' and then 'Courses'), I leave the fine tuning to you...

And we have to make sure we call that on click:

function clickFct(d,i) {
  adjustPointerEvents(d.depth);
  ...

And we're all set.

Here's the updated jsfiddle: Fiddle

As mentioned above, some fine tuning is still needed, but I hope I provided some more pointers.

You can dynamically change the pointer-events style of the node based on the zoom level. So in your .enter() when the node is created set .style("pointer-events", "none") and then when that node is zoomed in, something like

d3.select("some css for identifying the node")
 .style("pointer-events", "auto")

Depending on how you're doing the zoom, this would need to be adjusted (for instance, if clicking on the node at first zooms it in and then after that clicking on the node opens the page, in which case you'd need to change the .on("click") behavior. If you want to avoid inline styles (and you should) you could have the base class of your nodes have pointer-events: none a class of clickable which has pointer-events: auto and you can use d3.classed("clickable", true) in your zoom function to make click behavior available.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!