简体   繁体   English

Javascript触摸事件故障

[英]Javascript touch events glitchy

I'm currently creating some sliders in Javascript, for use with touch, and the results I'm getting aren't all that I'd hoped for. 我目前正在使用Javascript创建一些滑块,用于触摸,我得到的结果并非我所希望的。

When holding down one of the slider handles, it gets a translucent dark grey overlay (like all links do when you hold them down on iOS). 当按住其中一个滑块手柄时,它会获得半透明的深灰色覆盖(就像在iOS上按住它们时所做的所有链接一样)。 Once it's done this, the handle can't be dragged. 完成此操作后,无法拖动手柄。

This makes them exceptionally difficult to use, as you can imagine. 正如您所想象的那样,这使得它们非常难以使用。

Another problem I'm having is with the page scrolling. 我遇到的另一个问题是页面滚动。 Unless I get a near-perfect horizontal swipe, iOS safari thinks I'm trying to scroll the page and stops the js. 除非我得到近乎完美的横向滑动,否则iOS Safari会认为我正在尝试滚动页面并停止js。

I know its possible to fix these problems, because jQuery mobile doesn't have them with their slider. 我知道有可能解决这些问题,因为jQuery mobile没有滑块。 I've been through the source code for the jQuery mobile slider, but I can't find how they're preventing these problems. 我已经浏览了jQuery移动滑块的源代码,但我找不到它们如何防止这些问题。

Any ideas on what I can do to fix this? 关于我能做些什么来解决这个问题的任何想法?

jQuery mobile's slider - http://jquerymobile.com/demos/1.1.0/docs/forms/slider/ jQuery mobile的滑块 - http://jquerymobile.com/demos/1.1.0/docs/forms/slider/

Problem 1 - Grey Box on click: 问题1 - 点击灰色框:

Add this css for all elements: 为所有元素添加此css:

-webkit-tap-highlight-color: rgba(0,0,0,0);

You might also want to use these as well to stop highlighting: 您可能还想使用这些来停止突出显示:

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

Problem 2 - Stop Scrolling 问题2 - 停止滚动

You'll need a handler for the events touchstart and touchend and you'll need to add this to the handlers. 你需要一个事件touchstart和touchend的处理程序,你需要将它添加到处理程序。

event.preventDefault();

Do note this will stop scrolling and zooming as well. 请注意,这也将停止滚动和缩放。 If you still want zooming you will likely need to re-implement or use an additional library. 如果您仍想要缩放,则可能需要重新实现或使用其他库。

Good reference info on touch events 关于触摸事件的良好参考信息

Tutorial: http://www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/ 教程: http//www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/

Apple Docs (this has a good explanation on the relation between touch and mouse events): https://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html Apple Docs(这对触摸和鼠标事件之间的关系有一个很好的解释): https//developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html

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

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