本示例演示单指操作滚动页面,双指或者按住(Cmd or Ctrl)操作才控制地图.
本示例演示页面滚动的习惯操作:在触屏设备上,单个手机操作的时候,只滚动页面。当两个手机一起拖拽的时候,才可以操作地图。 对于鼠标或者触摸板,只有按住 (Cmd 或者 Ctrl) 键的时候才会拖动缩放地图,否则会滚动页面。
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {DragPan, MouseWheelZoom, defaults} from 'ol/interaction.js';
import {platformModifierKeyOnly} from 'ol/events/condition.js';
const map = new Map({
interactions: defaults({dragPan: false, mouseWheelZoom: false}).extend([
new DragPan({
condition: function (event) {
return this.getPointerCount() === 2 || platformModifierKeyOnly(event);
},
}),
new MouseWheelZoom({
condition: platformModifierKeyOnly,
}),
]),
layers: [
new TileLayer({
source: new OSM(),
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
}),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Panning and page scrolling</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="./resources/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "two-finger-pan-scroll",
"dependencies": {
"ol": "7.3.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}