Skip to content

Commit

Permalink
Merge pull request openlayers#7904 from ahocevar/focus-scroll
Browse files Browse the repository at this point in the history
Add focus condition, e.g. for wheel zoom
  • Loading branch information
ahocevar committed Feb 28, 2018
2 parents 85d4642 + 2d67c15 commit cf0c42a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
20 changes: 13 additions & 7 deletions examples/mousewheel-zoom.html
Original file line number Diff line number Diff line change
@@ -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 <code>constrainResolution: true</code>
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"
---
<div id="map" class="map"></div>
<div tabindex="1" id="map" class="map"></div>
4 changes: 3 additions & 1 deletion examples/mousewheel-zoom.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: [
Expand Down
13 changes: 13 additions & 0 deletions src/ol/events/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<div id="map" tabindex="1">`.
*
* @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.
*
Expand Down

0 comments on commit cf0c42a

Please sign in to comment.