leaflet: don't fire click event function on doubleclick

后端 未结 3 1753
庸人自扰
庸人自扰 2021-01-03 08:51

I have a question concerning clicks on a map in leaflet. If I click on the map I want to set a marker there, but if doubleclick on the map I just want to zoom in without set

3条回答
  •  耶瑟儿~
    2021-01-03 09:18

    Had the same problem of 2 x unwanted click events firing when listening for dblclick.

    Note: I wanted single and double clicks on the same element to perform different actions.

    I adapted this approach to my leaflet map, which is not bullet proof but eliminates 99% of the conflicts:

    var timer = 0;
    var delay = 200;
    var prevent = false;
    
    $("#target")
      .on("click", function() {
        timer = setTimeout(function() {
          if (!prevent) {
            doClickAction();
          }
          prevent = false;
        }, delay);
      })
      .on("dblclick", function() {
        clearTimeout(timer);
        prevent = true;
        doDoubleClickAction();
      });
    

    Credit: CSS-Tricks

    Code Pen Example

提交回复
热议问题