diff --git a/examples/mousewheel-zoom.html b/examples/mousewheel-zoom.html index 64ac29f801..1e20bba4da 100644 --- a/examples/mousewheel-zoom.html +++ b/examples/mousewheel-zoom.html @@ -1,12 +1,18 @@ --- layout: example.html title: Mousewheel/Trackpad Zoom -shortdesc: Restrict wheel/trackpad zooming to integer zoom levels. +shortdesc: Shows advanced wheel/trackpad zoom options. docs: > - By default, the `ol.interaction.MouseWheelZoom` can leave the map at - fractional zoom levels. If instead you want to constrain wheel/trackpad - zooming to integer zoom levels, set constrainResolution: true - when constructing the interaction. -tags: "trackpad, mousewheel, zoom, interaction" + This example uses a custom `ol.interaction.MouseWheelZoom` configuration: + + * By default, wheel/trackpad zoom is always active, which can be unexpected + on pages with a lot of scrollable content and an embedded map. To perform + wheel/trackpad zoom actions only when the map has the focus, set + `condition: ol.events.condition.focus` as constructor option. This requires + a map div with a `tabindex` attribute set. + * By default, the interaction can leave the map at fractional zoom levels. If + instead you want to constrain wheel/trackpad zooming to integer zoom + levels, set `constrainResolution: true`. +tags: "trackpad, mousewheel, zoom, scroll, interaction" --- -
+
diff --git a/examples/mousewheel-zoom.js b/examples/mousewheel-zoom.js index 1a97f7ba54..2035d9fe49 100644 --- a/examples/mousewheel-zoom.js +++ b/examples/mousewheel-zoom.js @@ -1,6 +1,7 @@ import Map from '../src/ol/Map.js'; import View from '../src/ol/View.js'; import {defaults as defaultInteractions} from '../src/ol/interaction.js'; +import {focus} from '../src/ol/events/condition.js'; import MouseWheelZoom from '../src/ol/interaction/MouseWheelZoom.js'; import TileLayer from '../src/ol/layer/Tile.js'; import OSM from '../src/ol/source/OSM.js'; @@ -9,7 +10,8 @@ import OSM from '../src/ol/source/OSM.js'; const map = new Map({ interactions: defaultInteractions({mouseWheelZoom: false}).extend([ new MouseWheelZoom({ - constrainResolution: true // force zooming to a integer zoom + constrainResolution: true, // force zooming to a integer zoom + condition: focus // only wheel/trackpad zoom when the map has the focus }) ]), layers: [ diff --git a/src/ol/events/condition.js b/src/ol/events/condition.js index 698a4432c7..c280709650 100644 --- a/src/ol/events/condition.js +++ b/src/ol/events/condition.js @@ -41,6 +41,19 @@ export const altShiftKeysOnly = function(mapBrowserEvent) { }; +/** + * Return `true` if the map has the focus. This condition requires a map target + * element with a `tabindex` attribute, e.g. `
`. + * + * @param {ol.MapBrowserEvent} event Map browser event. + * @return {boolean} The map has the focus. + * @api + */ +export const focus = function(event) { + return event.target.getTargetElement() === document.activeElement; +}; + + /** * Return always true. *