d3使用按钮进行缩放并双击,但不要使用滚轮进行缩放

时间:2016-06-07 19:03:08

标签: javascript html d3.js svg

我使用this example作为缩放项目的基础,但我想将缩放限制为仅按钮并双击。理想情况下,使用滚轮应该继续向下滚动页面,而不是在鼠标悬停在svg上时放大。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:4)

对于D3 4.0,有两种方法可以防止d3变焦响应轮子事件。

A:

zoom.filter(function() { return !event.button && event.type !== 'wheel'; })
// the `!event.button` expression is from the default filter, according to
// doc for zoom.filter([filter])

B:

svg.on("wheel.zoom", null);
// according to doc for zoom(selection): Internally, the zoom behavior        
// uses selection.on to bind the necessary event listeners for zooming. 
// The listeners use the name .zoom

鼠标拖动仍然可以平移(翻译)。

答案 1 :(得分:1)

您可以通过以下方式实现这一目标:

var svg = d3.select("body").select("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
   //.call(zoom) //remove the zoom listener from the group.
  .append("g");

接下来,在圆圈和矩形上附加doublclick侦听器以进行缩放。

d3.selectAll("rect").on('dblclick', zoomClick)
d3.selectAll("circle").on('dblclick', zoomClick)

工作代码here

相关问题