简体   繁体   English

ReactJS Touch事件和iOS Safari

[英]ReactJS Touch Events and iOS Safari

I'm trying to build a react component that is a swipeable carousel. 我正在尝试构建一个可滑动轮播的react组件。 This is aimed at mobile devices so touch is a must. 这是针对移动设备的,因此触摸是必须的。 Here is my current setup: 这是我当前的设置:

  1. A new react class with an object full of shared variables like so: 一个新的React类,其对象充满共享变量,如下所示:

     var pmilla = React.createClass({ ppSlick: { touchStartVal: '', prevTouchVal: '', matrix: '', initSliderLeft: '', threshHold: 75, // number of pixels we need to travel before moving to next slide timestamp: '', slideTrack: '', // should be a jquery object viewPortWidth: '', offSets: '', disFromStart: '', isSwiping: false, currentSubPage: '', animType: '', transformType:'', transitionType:'', }, 
  2. Touchstart handler: Touchstart处理程序:

     touchStartHandler: function(e){ this.ppSlick.touchStartVal = e.nativeEvent.touches[0]; this.ppSlick.prevTouchVal = e.nativeEvent.touches[0]; this.ppSlick.matrix = this.transMatrixToArray(this.ppSlick.slideTrack.css(this.ppSlick.transformType)); this.ppSlick.initSliderLeft = parseInt(this.ppSlick.matrix[4] , 10); }, 
  3. Touchmove handler (and this is where the problem lies): Touchmove处理程序(这就是问题所在):

     touchMoveHandler: function(e){ var touch = e.nativeEvent.touches[0]; this.ppSlick.disFromStart = e.nativeEvent.touches[0].pageX - this.ppSlick.touchStartVal.pageX; var dis = Number(e.nativeEvent.touches[0].pageX - this.ppSlick.prevTouchVal.pageX); console.log('from pp obj', this.ppSlick.prevStartVal); console.log('inside touchandler', touch); 

So far so good. 到现在为止还挺好。 Everything is working nicely on desktop browsers as can be seen in this screenshot. 在此屏幕截图中可以看到,一切在桌面浏览器上都运行良好。 (This is the output from the two console logs; the difference between the two pageX values is what I use to handle the transform) (这是两个控制台日志的输出;两个pageX值之间的差异是我用来处理转换的内容)

OS X Chrome

So all of this works up until this point. 因此,所有这些工作到现在为止。 Now see what happens on iOS Safari: 现在看看在iOS Safari上会发生什么:

iOS 8 Safari

They're the same. 他们是一样的。 I've tried using different touch events (nativeEvent vs Synthetic), I've tried placing the touchStartVal and prevTouchVal variables elsewhere and nothing seems to help. 我尝试使用不同的触摸事件(nativeEvent与Synthetic),尝试将touchStartValprevTouchVal变量放置在其他位置,但似乎无济于事。 I'm out of ideas on this one. 我对此一无所知。 Let me know if I should provide more info on this. 让我知道是否需要提供更多信息。

Thanks! 谢谢!

It could be that it's iOS safari bug. 可能是iOS的Safari浏览器错误。

try with: 尝试:

cursor: pointer;

more info: 更多信息:

http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html

The solution is to not use some arbitrary object to shared variables but setState(). 解决方案是不要对共享变量使用某些任意对象,而要使用setState()。 I'm not entirely sure what React is doing under the hood but it looks like the global object I had defined kept getting overwritten. 我不能完全确定React在做什么,但是看起来我定义的全局对象一直被覆盖。 Using the components state worked. 使用组件状态有效。

Oddly enough though, the previous implementation (object) worked on desktop browsers but broke on iOS Safari. 奇怪的是,先前的实现(对象)在桌面浏览器上可以运行,但在iOS Safari上无法运行。 Still not sure why. 仍然不确定为什么。

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

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