openlayers禁用鼠标滚轮放大、拖拽事件并设置为只有按住Ctrl键才可以执行放大拖拽操作

 禁用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值