Leaflet mouse wheel zoom only after click on map
It's simple: create L.Map with scrollWheelZoom: false
option, then add a listener:
map.once('focus', function() { map.scrollWheelZoom.enable(); });
If you need to toggle zooming:
map.on('click', function() {
if (map.scrollWheelZoom.enabled()) {
map.scrollWheelZoom.disable();
}
else {
map.scrollWheelZoom.enable();
}
});
More of a comment/improvement on the toggle component of the accepted answer, which is great (thanks). But.
When interacting with a map, for many use cases the user also needs to click the map to perform their task, so this:
map.on('click', function() {
if (map.scrollWheelZoom.enabled()) {
map.scrollWheelZoom.disable();
}
else {
map.scrollWheelZoom.enable();
}
});
May result in some unexpected behaviour once the user starts actually using the map.
I would suggest something that may seem a bit more intuitive the user - click off the map to disable mouse scrolling.
For example set your scrollWheelZoom: false
as above, then:
map.on('focus', function() { map.scrollWheelZoom.enable(); });
map.on('blur', function() { map.scrollWheelZoom.disable(); });
Leaflet.Sleep will make your job easy, and it's plenty configurable
Basically, it turns off scroll events when they're not needed and "wakes" your map when they are.
I'd post code, but the defaults seem to get it right, so you likely won't need anything beyond <script src="path/to/leaflet-sleep.js"></script>
and you'll have a map like this.