简体   繁体   English

在Android浏览器中禁用触发touchcancel

[英]Disable the firing of touchcancel in Android Browser

I am making a mobile website that uses JavaScript touch events. 我正在制作一个使用JavaScript触摸事件的移动网站。 Things work fine in iOS Safari and Chrome for Android, but the stock Android Browser (version 4.1.2) is giving me trouble. 在iOS Safari和适用于Android的Chrome浏览器中,一切正常,但现有的Android浏览器(版本4.1.2)给我带来麻烦。

During a touch process, the touchstart and touchmove events are called as expected. 在触摸过程中, touchstart预期调用touchstarttouchmove事件。 However, one of the actions performed by the touchmove handler seems to trigger a premature touchcancel event. 但是, touchmove处理程序执行的动作之一似乎触发了过早的touchcancel事件。 (I'm not sure whether this is significant, but the action that triggers the touchcancel is the modification of an SVG object's viewBox attribute.) If I comment out this action, the touch process proceeds normally (ie, completion of touchmove through to touchend ). (我不确定这是否很重要,但是触发touchcancel是对SVG对象的viewBox属性的修改。)如果我注释掉此操作,则触摸过程将正常进行(即,完成touchmove through至touchend )。

All of my touch handlers call the preventDefault() function, so the issue isn't the one that's described in this bug: https://code.google.com/p/android/issues/detail?id=19827 . 我所有的触摸处理程序都调用了preventDefault()函数,因此问题不是此Bug中描述的问题: https : //code.google.com/p/android/issues/detail?id=19827

I've read that there is a lot of inconsistency among browsers as to when touchcancel is called. 我已经阅读到,在touchcancel调用touchcancel 浏览器之间存在很多不一致之处 The stock Android browser is the only one that is problematic for me. 现有的Android浏览器是唯一对我有问题的浏览器。

Is there a workaround out there? 有没有解决方法? For example, is there away I can completely disable the touchcancel event? 例如,是否可以完全禁用touchcancel事件? Another idea I had was to have the touchcancel handler programmatically trigger another touchstart / touchmove event, but I didn't get very far with that. 我的另一个想法是让touchcancel处理程序以编程方式触发另一个touchstart / touchmove事件,但是我对此并不了解。 Any ideas would be appreciated. 任何想法,将不胜感激。

I know it's a bit late but if someonee else is facing this issue, here's a small jQuery extension that explains how to deal with pointercancel and touchcancel events. 我知道已经有点晚了,但是如果其他人面临这个问题, 这里有一个小的jQuery扩展 ,它解释了如何处理pointercanceltouchcancel事件。

Basically, if you switch to pointer events you will be able to prevent pointercancel by simply using the touch-action CSS property with a value of none , as long as the browser has both features correctly implemented. 基本上,如果切换到指针事件 ,只要浏览器正确实现了这两种功能,则只需使用值为nonetouch-action CSS属性就可以防止pointercancel (Android 5+, Chrome 55+, IE11, Edge, etc) (Android 5以上版本,Chrome 55以上版本,IE11,Edge等)

If you really must use the legacy touch events instead, you'll have to implement event.preventDefault() in your touchmove events and this will prevent touchcancel from firing, but it will also disable entirely the browser handling of any default action like a pan or click. 如果确实必须使用传统的触摸事件 ,则必须在touchmove事件中实现event.preventDefault() ,这将防止touchcancel ,但也将完全禁用浏览器对任何默认操作(如平移)的处理或单击。

As a final note, I wouldn't use touch events + touch-action CSS rules because touch-action was only recently added, at the same that that Pointer Events. 最后一点,我不会使用触摸事件+触摸动作CSS规则,因为touch-action只是在最近才添加的,与指针事件相同。 So while that combination may work in newer browsers , it will most certainly fail in older ones (by triggering touchcancel event unexpectedly). 因此,尽管该组合可能在较新的浏览器中起作用 ,但在较旧的浏览器中它肯定会失败 (通过意外触发touchcancel事件)。

Check the README from the jQuery extension I posted because it explains the implications of using either TouchEvent and PointerEvent interfaces. 从我发布的jQuery扩展中检查自述文件,因为它解释了使用TouchEvent和PointerEvent接口的含义。

If you are useing hammer.js, you can disable pointer-events (which cause problems like this), by adding: delete window.PointerEvent; 如果您使用的是Hammer.js,则可以通过添加以下命令来禁用指针事件(这会导致此类问题): delete window.PointerEvent; in your index.html PRIOR to loading hammer.js, and it will ignore those events. 在您加载index.html之前先加载Hammer.js,它将忽略这些事件。
You can also set SUPPORT_POINTER_EVENTS = false; 您还可以设置SUPPORT_POINTER_EVENTS = false; (line 384 of v2.0.8) to do the same thing. (v2.0.8的384行)做同样的事情。 Ideally the devs would add the ability to turn this off, but so goes the open source dilemma... 理想情况下,开发人员将添加关闭此功能的功能,但是开源困境也是如此。

I think touchcansel is special event that triggers when user touched button in specific behavior. 我认为touchcansel是一种特殊事件,当用户以特定行为触摸按钮时触发。

Ex. 例如 User touch button, but didn't removed his finger from display on exactly that button, instead of this he could move it a bit to the left, and end his action there. 用户触摸按钮,但没有将手指从该按钮上的显示中完全移开,相反,他可以将其向左移动一点,然后在此处结束操作。 In that case touchcancel would appear. 在这种情况下,将出现touchcancel。

So if you don't want that action to fire, i think you have to remove all handlers that connected with that action in android browser. 因此,如果您不希望触发该动作,我认为您必须在android浏览器中删除与此动作相关的所有处理程序。

$('selector').off('touchcancel'), of if it's delefated $('parent').undelegate('touchcancel','selector') $('selector')。off('touchcancel'),如果是$('parent')。undelegate('touchcancel','selector')

I don't think that there is problem with e.preventDefault(). 我认为e.preventDefault()没有问题。 Only may be if you click on a link, and page starts to reload. 仅当您单击链接后,页面才开始重新加载。 May be page is reloaded when you click on button? 单击按钮可能会重新加载页面吗? and you wait for some actions after page reloaded, you actually may not understand, that it happend. 并且您在页面重新加载后等待一些操作,您实际上可能不知道它发生了。

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

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