简体   繁体   English

如何防止Mousewheel事件的默认操作

[英]How to Prevent Default action of Mousewheel event

I'm working on building a d3.js bar chart that will allow me to scroll on the x axis by wrapping an svg element with fixed width and heigh inside of smaller div with overflow properties. 我正在构建d3.js条形图,通过将svg元素固定为固定宽度并将其高度封装在具有溢出属性的较小div内,使我可以在x轴上滚动。 This visualization is being built on a platform that handles the data and provides a javascript code editor to create the visualization. 该可视化文件是在处理数据的平台上构建的,并提供了一个JavaScript代码编辑器来创建可视化文件。 Once you publish the code, the bar chart renders in an html div element. 发布代码后,条形图将在html div元素中呈现。

The page where the bar chart gets rendered has a default zoom event that fires when you use the mousewheel. 条形图呈现的页面具有默认的缩放事件,该事件在您使用鼠标滚轮时触发。 Since I would like to scroll in my bar chart by using the mousewheel, I'm trying to stop the default zoom action from firing by included the following statement in the code: 由于我想使用鼠标滚轮滚动条形图,因此尝试通过在代码中包含以下语句来阻止触发默认的缩放操作:

$(vizhtmlelement).mousewheel(function() {
    return false;
});

This works successfully at removing the zoom functionality, but it also prevents the scrolling with the mousewheel from working. 这可以成功删除缩放功能,但同时也无法使用鼠标滚轮进行滚动。 Is there a way I can stop the mousewheel zoom from happening while keeping the mousewheel scroll in my visualization? 有什么方法可以阻止鼠标滚轮缩放同时将鼠标滚轮保持在可视化状态?

On whatever element you .call() your zoom function, you need to set its mousewheel behavior to null. 在.call()缩放功能的任何元素上,都需要将其鼠标滚轮行为设置为null。 So if you .call(zoom) from a or with the id of "yourElement" then it would look like this: 因此,如果您从或ID为“ yourElement”的.call(zoom)看起来像这样:

d3.select("#yourElement")
.call(myZoom)
.on("dblclick.zoom", null)
.on("mousewheel.zoom", null)
.on("DOMMouseScroll.zoom",null);

Notice I also disabled the double-click zoom, because you'll probably want that disabled as well. 注意,我也禁用了双击缩放,因为您可能也希望禁用它。

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

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