touchStart 的 DragStart event.datatransfer.setData 等效项是什么?
在反应中,我有一个想要拖放的元素。现在我有一个dragStart和dragEnd,它们使用ev.dataTransfer.setData(data)将我的元素拖放到另一个框中。
但是,我也希望能够在触摸屏上执行此操作。当我使用触摸屏时,dragStart 不执行任何操作。我发现我应该使用 TouchStart 和 TouchEnd 作为触摸屏。但是,touchStart 没有 dataTransfer 事件。我想知道相当于什么?
如果没有等效的方法,那么我在触摸屏上拖放元素的好方法是什么?
谢谢
In react I have an element that I want to drag and drop. Right now I have a dragStart and dragEnd that uses ev.dataTransfer.setData(data) to drag and drop my element to a different box.
However, I also want to be able to do this on a touch screen. When I use a touchscreen, the dragStart doesn't do anything. I figured out instead that I should use TouchStart and TouchEnd for the touchscreen. However, there is no dataTransfer event with touchStart. I'm wondering what the equivalent would be?
If there is no equivalent, what would be a good way for me to drag and drop my elements on touchscreen?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Reactjs 对我来说有点陌生,但通常在普通中,触摸数据传输可以通过将数据存储到对象/变量中来完成。基本上,这就是拖动元素时 dataTransfer 所做的全部工作。
为此,在 ontouchstart 事件中,像使用 dataTransfer.setData() 一样存储要携带的数据,然后通过复制元素来模拟元素的拖动。
然后在ontouchmove事件中,可以让复制的元素跟随光标,模拟拖动效果。在 touchmove 事件中,我们可以检测到光标位于 dropzone 元素内,就像 DnD 的 onDrop 事件一样。这是DnD帮助平滑体验的手动部分,但是对于触摸,我们需要自己进行检测。
最后,在 ontouchend 事件中,我们执行 dataTransfer.getData() 的逻辑。因为我们已经检测到光标是否在拖放区域内,所以我们可以相应地执行逻辑
下面,是我在 Reactjs 中传递此内容的尝试
Reactjs is abit alien to me, but generally in vanilla, dataTransfer for touch can be done by storing your data into object/variable. Basically that's all dataTransfer do while you drag an element.
To do this, ontouchstart event, store the data that you want to carry like you did with dataTransfer.setData(), then simulate the dragging of an element by duplicating it.
then in ontouchmove event, you can have the duplicated element to follow cursor, to simulate the dragging effect. while on touchmove event, we can detect either our cursor is inside the dropzone element, like you would normally have as onDrop event for DnD. this is the manual part that DnD has helped to smoothen the experience, but for touch, we need to do the detection by ourself.
finally, in our ontouchend event, we do the logic as we have for our dataTransfer.getData(). Because we already detect if our cursor is within the dropzone or not, so we can perform the logic accordingly
Below, is my attempt on having this delivered in Reactjs