在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
in D3.js is there any way bind the scroll event in both the x and y directions to panning the svg?
当前,每当我向上或向下滚动时,我的d3树都有缩放行为。我想用平移来代替这种行为。有人知道我会怎么做吗?
// Define the zoom function for the zoomable tree
function zoom() {
svgGroup.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// define the zoomListener which calls the zoom function on the "zoom" event constrained within the scaleExtents
var zoomListener = d3.behavior.zoom().scaleExtent([0.1, 3,1]).on("zoom", zoom);
// define the baseSvg, attaching a class for styling and the zoomListener
var baseSvg = d3.select("#tree-container").append("svg")
.attr("width", viewerWidth)
.attr("height", viewerHeight)
.attr("class", "overlay")
.call(zoomListener);
以下是我将默认缩放功能更改为平移的解决方案。我能找到各种各样的资源,包括一个关于stackoverflow的好帖子。
function panTo() {
var current_translate = d3.transform(svgGroup.attr("transform")).translate;
var dx = d3.event.wheelDeltaX + current_translate[0];
var dy = d3.event.wheelDeltaY + current_translate[1];
svgGroup.attr("transform", "translate(" + [dx,dy] + ")");
d3.event.stopPropagation();
}
// Define the zoom function for the zoomable tree
function zoom() {
svgGroup.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// define the zoomListener which calls the zoom function on the "zoom" event constrained within the scaleExtents
var zoomListener = d3.behavior.zoom().scaleExtent([0.1, 3,1]).on("zoom", zoom);
// define the baseSvg, attaching a class for styling and the zoomListener
var baseSvg = d3.select("#tree-container").append("svg")
.attr("width", viewerWidth)
.attr("height", viewerHeight)
.attr("class", "overlay")
.call(zoomListener)
.on("wheel.zoom",panTo)
.on("mousewheel.zoom", panTo)
.on("DOMMouseScroll.zoom", panTo)
相关文章:
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- fullpage.js在滚动时更改滚动方向
- 如何使用javascript检测不可滚动元素中的滚动事件和方向
- 滚动功能工作方向错误
- 更改滚动Horz的滑动方向
- 确定适合窗口的页面的滚动方向
- 将页面分成两部分,并将两个部分滚动到相反的方向
- 根据滚动方向和高度加载和删除内容页面
- 鼠标滚轮滚动上的滑块 - 方向右 - 左
- 检测禁用橡皮筋效果的页面中的滚动方向
- 使用jquery在同一方向上连续滚动滑块
- 检查'鼠标滚轮'滚动锁定的事件方向
- 如何在ScrollStart上使用Iscroll获得滚动方向
- 使用setTimeout查找滚动方向
- InternetExplorer9及以下版本,当在主体上使用rtl方向时,将滚动条设置为右侧
- 溢出时移动滚动方向检测:隐藏页面
- 如何在使用溢出后获得滚动方向:自动禁用动量滚动
- 手机上的滚动方向
- 我如何反转滚动方向上的谷歌图表
- 如何确定滚动方向,而不实际滚动