简体   繁体   English

JavaScript移动拖放

[英]JavaScript Mobile Drag and Drop

I was testing out an interface I built using jQuery UI draggable, droppable and sortable and noticed that the drag and drop doesn't work in the mobile browser. 我正在测试我使用jQuery UI draggable,droppable和sortable构建的接口,并注意到拖放在移动浏览器中不起作用。 I suppose there is a different way in which events are fired in the mobile browser. 我想有一种不同的方式可以在移动浏览器中触发事件。

Are there any JavaScript frameworks yet, that support drag and drop in the mobile browser? 还有任何JavaScript框架,支持拖放移动浏览器吗?

If not, any ideas on how one could go about putting together some basic drag/drop functionality? 如果没有,关于如何组合一些基本的拖放功能的任何想法?

A tricky one...I think you've pretty much highlighted the problem: the touch events aren't integrated with the jQuery drag/drop functionality yet, and I'm not sure if there are drag/drop libraries out there targeting the mobile device range. 一个棘手的问题...我认为你已经非常突出了这个问题:触摸事件还没有与jQuery拖放功能集成,我不确定是否有拖拽库来定位移动设备范围。

What I can say is that you can start tapping into those events yourself, but I'm not sure if you're willing to write all that yourself. 我可以说你自己可以开始接触这些事件,但我不确定你是否愿意自己写这些事。 In simple terms, checking for a mobile device and then attaching the events could be achieved like this: 简单来说,检查移动设备然后附加事件可以这样实现:

if (navigator.userAgent.match(/iphone/i) ||
    navigator.userAgent.match(/ipad/i) ||
    navigator.userAgent.match(/ipod/i) ||
    navigator.userAgent.match(/android/i))
{
    var element = $('#your_draggable_element_id');
    element.bind('touchstart', touchStartFunctionHere);
    element.bind('touchmove', touchMoveFunctionHere);
    element.bind('touchend', touchEndFunctionHere);
}

That would patch you into the events, which would give you control over what you did next. 这会让你对事件进行补丁,这可以让你控制下一步做什么。 You'd obviously want the locations of drag moves, touches, etc., and you can get those from the event itself. 你显然想要拖动移动,触摸等的位置,你可以从事件本身获得。 For example, based on the above code: 例如,基于以上代码:

function touchMoveFunctionHere(event)
{
    event.preventDefault();
    var x = event.originalEvent.touches[0].pageX;
    var y = event.originalEvent.touches[0].pageY;

    ...

}

Not the complete solution you were looking for, and I hope someone else can shed some light on drag/drop frameworks for mobile devices, but it's a start! 不是你想要的完整解决方案,我希望其他人可以为移动设备的拖放框架提供一些启示,但这是一个开始! Hope this helps! 希望这可以帮助!

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

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