简体   繁体   English

Fastclick.js导致Leaflet.js点击被忽略

[英]Fastclick.js causes Leaflet.js clicks to be ignored

I am developing a Cordova app which has a map built with Leafletjs at the heart of it. 我正在开发一个Cordova应用程序,它的核心是Leafletjs构建的地图。 The map has markers which when clicked pop up an info box. 地图上有标记,点击后会弹出一个信息框。

I want to get rid of the 300ms delay on links around the site in general - basically on all of the anchors (a tags). 我想摆脱网站周围链接的300毫秒延迟 - 基本上在所有锚点(标签)上。 I don't need to apply it to the Leafletjs markers because there is currently no delay when a user taps a marker. 我不需要将它应用于Leafletjs标记,因为当用户点击标记时,当前没有延迟。

I have installed fastclick ( https://github.com/ftlabs/fastclick/ ) - it removed the delay from the a tags brilliantly - however it created problems on the Leafletjs markers which now sometimes need two cicks to fire. 我已经安装了fastclick( https://github.com/ftlabs/fastclick/ ) - 它很好地消除了标签的延迟 - 但是它在Leafletjs标记上产生了问题,现在有时需要两个标记才能触发。

I have tried adding the class 'needsclick' on the markers Leafletjs creates which according to the fastclick documentation should make Fastclick ignore them - however it does not seem to have any affect. 我已经尝试在Leafletjs创建的标记上添加类'needsclick',根据fastclick文档应该使Fastclick忽略它们 - 但它似乎没有任何影响。 (Example:) (例:)

$('.leaflet-marker-icon').addClass('needsclick');
$(function() {
    FastClick.attach(document.body);
});

As the leafletjs markers click events are on img rather than a tags, if I could attach Fastclick to only a tags I think this would fix my issue, however I have tried passing a tags as the layer to Fastclick but this doesn't work either. 由于leafletjs标记单击事件是img而不是标签,如果我可以将Fastclick仅附加到标签我认为这将解决我的问题,但是我尝试将标签作为图层传递给Fastclick但这不起作用。 (Example:) (例:)

$(function() {
    var Anchors = document.getElementsByTagName("a");
    FastClick.attach(Anchors);
});

Here is a minimal jsfiddle demonstrating the behavior (requires iDevice): https://jsfiddle.net/y723oet0/2/ 这是一个演示行为的最小jsfiddle(需要iDevice): https ://jsfiddle.net/y723oet0/2/

If anyone has any advice, it would be appreciated. 如果有人有任何建议,将不胜感激。

fastclick.js keeps an internal variable called this.targetElement that keeps track of the element that you are currently tapping on. fastclick.js保留一个名为this.targetElement的内部变量,用于跟踪当前正在点击的元素。 When the touchend event fires, fastclick.js checks to see if the element that the touch ended on is the same as the element the touch began on. touchend事件触发时,fastclick.js会检查触摸结束的元素是否与触摸开始的元素相同。

On normal new clicks on the map, this.targetElement starts out as null and everything works properly. 在地图上的正常新点击时, this.targetElement以null开头,一切正常。 If the element is tapped quickly, then this.targetElement = <a> and is stuck on that state, which causes the fastclick.js internal function onMouse to reject the next map click. 如果快速点击该元素,则this.targetElement = <a>并停留在该状态,这会导致fastMlick.js内部函数onMouse拒绝下一个地图点击。

We worked around this by modifying fastclick.js as follows: 我们通过修改fastclick.js来解决这个问题,如下所示:

diff --git a/fastclick.js b/fastclick.js
--- a/fastclick.js
+++ b/fastclick.js
@@ -606,6 +606,8 @@
                        this.sendClick(targetElement, event);
                }

+               this.targetElement = null;
+
                return false;
        };

// end of patch - don't copy this line

This patch causes the this.targetElement variable to be cleared on the touchend event that began outside the map so that the next tap on the map does not get blocked. 此修补程序会导致this.targetElement变量在地图外部开始的touchend事件中清除,以便下一次点击地图不会被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM