简体   繁体   English

iPhone / iPad触摸事件Javascript LineChart

[英]iPhone/iPad touch events Javascript LineChart

I am trying to implement a linegraph with movable points in Javascript using Highcharts . 我正在尝试使用Highcharts在Javascript中实现带有可移动点的线图。 Using latest Chrome everything just works as expected. 使用最新的Chrome,一切都按预期工作。 However, when I look at it on my iPhone or iPad, the movable points wouldn't move at all. 但是,当我在iPhone或iPad上查看它时,可移动点根本不会移动。

This is due to the fact that mouse events are handled differently in mobile Safari. 这是因为鼠标事件在移动Safari中的处理方式不同。

mousedown becomes touchstart , mousemove becomes touchmove , etc. mousedown成为touchstartmousemove成为touchmove等。

I tried to map all the touchevents to their appropriate mobile equivalent but without much success. 我试图将所有的触摸屏映射到适当的移动设备,但没有太大的成功。 The points can be dragged, but the view is not updated... 可以拖动点,但视图不会更新...

Chrome working version: http://jsfiddle.net/MTyzv/3/ Chrome工作版: http//jsfiddle.net/MTyzv/3/

Mobile Safari version: http://jsfiddle.net/MTyzv/7/ 移动Safari版: http//jsfiddle.net/MTyzv/7/

UPDATE UPDATE

Ok I narrowed the problem down a bit... It seems like all the touch events are handled correctly, but the points jump to 0.0 as soon as they are moved. 好吧我把问题缩小了一点......似乎所有的触摸事件都被正确处理了,但是一旦移动它们就会跳到0.0。 In addition to that, the graph is not "redrawn" after the initial touch. 除此之外,初始触摸后图形不会“重新绘制”。 See the updated version of this Fiddle http://jsfiddle.net/MTyzv/11/ 查看此Fiddle的更新版本http://jsfiddle.net/MTyzv/11/

It's a usual issue for everyone who starts working with touch events. 这对于开始使用触摸事件的每个人来说都是一个常见的问题。

TouchEvent supports multitouch, so there's not such a thing like event.pageX . TouchEvent支持多点触控,因此不存在像event.pageX这样的事情。 TouchEvent contains three "TouchLists", its: TouchEvent包含三个“TouchLists”,它的:

  • event.touches event.touches
  • event.targetToches event.targetToches
  • event.changedTouches event.changedTouches

Finally on those lists you can get a Touch pageX, pageY, etc. If you don't care about multitouch there's a common simplification, you can call event.targetTouches[0].pageX 最后在这些列表上你可以得到一个Touch pageX,pageY等。如果你不关心多点触控,那么可以调用event.targetTouches [0] .pageX

I updated your demo here: http://jsfiddle.net/7UsbM/7/ now it works in both, touch-enabled devices and desktop browsers. 我在这里更新了你的演示: http//jsfiddle.net/7UsbM/7/现在它适用于支持触摸的设备和桌面浏览器。

Don't forget to take a look at MDN reference: https://developer.mozilla.org/en/DOM/Touch_events 不要忘记查看MDN参考: https//developer.mozilla.org/en/DOM/Touch_events

Hope it helps. 希望能帮助到你。

There is no such thing like drag for mobile, but you need to use touch events to this working. 没有像移动设备这样的东西,但你需要使用触摸事件来完成这项工作。 Usually you need x,y co-ordinates to make logic, so make functions/classes that work on x,y position of mouse/ touch, than simply add events and get touch x,y or click x,y and call these functions. 通常你需要x,y坐标来制作逻辑,所以制作适用于鼠标/触摸的x,y位置的函数/类,而不是简单地添加事件并获得触摸x,y或单击x,y并调用这些函数。

$('div').bind('mousedown' , function(e)
{
   // get x,y code
   var y = e.pageY;
   var x = e.pageX;

   // pass to function down(x,y);
   down(x,y);
});


$('div').bind('touchstart' , function(e)
{
   // get x,y code
   var touch = e.touches[0];
   var y = touch.pageY;
   var x = touch.pageX;

   // pass to function down(x,y);
   down(x,y);
});

function down(x,y)
{
   // do something with x,y
}

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

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