禁用openlayers鼠标滚轮放大、拖拽移动地图设置这两行代码即可:
new Map({
interactions: defaults({ dragPan: false, mouseWheelZoom: false }),
})
或者设置只允许按住Ctrl键才可以操作
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 (
// 检查是否有两个触点,或者按下了ctrl键
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,
}),
});
new MouseWheelZoom({ condition: platformModifierKeyOnly })
用于创建一个鼠标滚轮缩放交互- condition是一个配置项,用于指定触发缩放的条件。
- platformModifierKeyOnly是一个条件函数,仅在按住平台修饰键(如
Ctrl
键)时返回true
。