问题
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