jquery移动拖放

问题描述 投票:31回答:4

我正在玩jQuery UI的拖放功能,它正在我的网站上运行,但是当我在iPad上导航到我的网页时,div不会拖动 - 页面本身会上下移动。

我有头标记:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/le-frog/jquery-ui.css" type="text/css" media="all" />
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.load("jqueryui", "1");
google.setOnLoadCallback(init);
</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
jquery-mobile
4个回答
37
投票

Excellent sample solutions

jQuery UI Touch Punch is a small hack that enables the use of touch events on sites using the jQuery UI user interface library.

目前,jQuery UI用户界面库不支持在其小部件和交互中使用触摸事件。这意味着您在桌面浏览器中设计和测试的光滑UI将在大多数(如果不是全部)支持触摸的移动设备上失败,因为jQuery UI会监听鼠标事件 - 鼠标悬停,鼠标移动和鼠标移动 - 不触摸事件 - touchstart,touchmove和touchend。

这就是jQuery UI Touch Punch的用武之地.Touch Punch的工作原理是使用simulated eventstouch events映射到他们的鼠标事件类比。只需在页面上包含脚本,您的触摸事件就会变成相应的鼠标事件,jQuery UI将按预期响应。

正如我所说,Touch Punch是一个黑客。它duck punches jQuery UI的一些核心功能来处理触摸事件的映射...


18
投票

这个问题是已知的并且已经被研究过。

它需要在正确的事件处理程序中调用正确的.preventDefault()

你需要的一切都在这里:

jQuery - draggable images on iPad / iPhone - how to integrate event.preventDefault();?


13
投票

qazxsw poi包含一个优秀的代码片段,可将触摸事件转换为鼠标事件:

http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/

4
投票

Hammer.js提供了强大的拖动功能。 function touchHandler(event) { var touches = event.changedTouches, first = touches[0], type = ""; switch(event.type) { case "touchstart": type = "mousedown"; break; case "touchmove": type="mousemove"; break; case "touchend": type="mouseup"; break; default: return; } // initMouseEvent(type, canBubble, cancelable, view, clickCount, // screenX, screenY, clientX, clientY, ctrlKey, // altKey, shiftKey, metaKey, button, relatedTarget); var simulatedEvent = document.createEvent("MouseEvent"); simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null); first.target.dispatchEvent(simulatedEvent); event.preventDefault(); } function init() { document.addEventListener("touchstart", touchHandler, true); document.addEventListener("touchmove", touchHandler, true); document.addEventListener("touchend", touchHandler, true); document.addEventListener("touchcancel", touchHandler, true); }

© www.soinside.com 2019 - 2024. All rights reserved.