D3 event showing 'ReferenceError: event is not defined' in Firefox

喜你入骨 提交于 2019-12-13 01:04:01

问题


I am using mouse wheel zoom in my d3 Map.Following Code am using.

.on("wheel.zoom", function() {
    var currScale = projection41039.scale();
    var newScale = currScale - 1 * event.deltaY;
    if (newScale >= 150) {
        var currTranslate = projection41039.translate();
        var coords = projection41039.invert([event.offsetX, event.offsetY]);
        projection41039.scale(newScale);
        var newPos = projection41039(coords);
        projection41039.translate([currTranslate[0] + (event.offsetX - newPos[0]),
            currTranslate[1] + (event.offsetY - newPos[1])
        ]);
        updateContents();
    }
})

This works fine for Chrome, but throws an error in Firefox:

ReferenceError: event is not defined


回答1:


The issue here is that Chrome follows the Internet Explorer feature of Window.event, while Firefox doesn't:

For instance, given a button or any other element, the following snippet will work on Chrome, but not on Firefox:

d3.select("button").on("click", function() {
  console.log(event)
})

Try it here: https://jsfiddle.net/b9h4ntn0/ (I normally use Stack Snippets, but the snippet will freeze in that particular example)

Solution

Use d3.event. That way, you don't rely on Window.event:

d3.select("button").on("click", function() {
  console.log(d3.event)
})

The following code works both on Chrome and Firefox: https://jsfiddle.net/qj787zmj/



来源:https://stackoverflow.com/questions/48227127/d3-event-showing-referenceerror-event-is-not-defined-in-firefox

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