Wednesday, 24 July 2019

javascript - Leaflet mouse wheel zoom only after click on map


I'm working with the Leaflet JavaScript Library and attached a (working) map to my HTML Document. It is in the middle of the page, and when I'm scrolling down with my mouse wheel and arrive at the map, it automatically zooms into the map.


I want to scroll through the page without stopping at the map. Is there a way to activate the wheel zoom only after the first click on the map?



Answer



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();
}
});

No comments:

Post a Comment