我知道的老线程.......
问题与@likwid_t的答案是,它也阻止任何输入或其他元素,必须对'点击'作出反应(例如输入),所以我写了这个解决方案。该解决方案可以使用任何基于任何触摸设备(或cumputer)上的mousedown,mousemove和mouseup事件的现有拖放库。这也是一个跨浏览器的解决方案。
我已经测试过几种设备,它的工作速度很快(与ThreeDubMedia的拖放功能结合使用(另请参阅http://threedubmedia.com/code/event/drag))。这是一个jQuery解决方案,因此您只能在jQuery库中使用它。我使用了jQuery 1.5.1,因为有些新功能在IE9和以上(没有使用新版本的jQuery测试)无法正常工作。
在添加任何拖动或下降操作事件你必须调用这个函数首先:
simulateTouchEvents(<object>);
您还可以阻止所有组件/儿童为输入或加快事件处理使用以下语法:
simulateTouchEvents(<object>, true); // ignore events on childs
这是我写的代码。我使用了一些不错的技巧来加速评估事物(参见代码)。
function simulateTouchEvents(oo,bIgnoreChilds)
{
if(!$(oo)[0])
{ return false; }
if(!window.__touchTypes)
{
window.__touchTypes = {touchstart:'mousedown',touchmove:'mousemove',touchend:'mouseup'};
window.__touchInputs = {INPUT:1,TEXTAREA:1,SELECT:1,OPTION:1,'input':1,'textarea':1,'select':1,'option':1};
}
$(oo).bind('touchstart touchmove touchend', function(ev)
{
var bSame = (ev.target == this);
if(bIgnoreChilds && !bSame)
{ return; }
var b = (!bSame && ev.target.__ajqmeclk), // Get if object is already tested or input type
e = ev.originalEvent;
if(b === true || !e.touches || e.touches.length > 1 || !window.__touchTypes[e.type] )
{ return; } //allow multi-touch gestures to work
var oEv = (!bSame && typeof b != 'boolean')?$(ev.target).data('events'):false,
b = (!bSame)?(ev.target.__ajqmeclk = oEv?(oEv['click'] || oEv['mousedown'] || oEv['mouseup'] || oEv['mousemove']):false):false;
if(b || window.__touchInputs[ev.target.tagName])
{ return; } //allow default clicks to work (and on inputs)
// https://developer.mozilla.org/en/DOM/event.initMouseEvent for API
var touch = e.changedTouches[0], newEvent = document.createEvent("MouseEvent");
newEvent.initMouseEvent(window.__touchTypes[e.type], true, true, window, 1,
touch.screenX, touch.screenY,
touch.clientX, touch.clientY, false,
false, false, false, 0, null);
touch.target.dispatchEvent(newEvent);
e.preventDefault();
ev.stopImmediatePropagation();
ev.stopPropagation();
ev.preventDefault();
});
return true;
};
做些什么: 起初,它转换单点触摸事件转换成鼠标事件。它检查事件是否由必须拖动的元素上/上的元素引起。如果它是input,textarea等input元素,它会跳过翻译,或者如果标准鼠标事件附加到它,它也会跳过翻译。
结果: 可拖动元素上的每个元素仍在工作。
快乐编码,格尔茨, 欧文Haantjes
的[可能重复我怎样才能让一个jQuery UI“拖动()'div draggable for touchscreen?](http://stackoverflow.com/questions/3026915/how-can-i-make-a-jquery-ui-draggable-div-draggable-for-touchscreen) – ahsteele 2011-04-26 20:41:36
这确实是我们研究过的SO帖子之一,但在帖子中引用的可拖拽修复程序没有为我们工作。 – Pat 2011-04-26 21:17:51
我们看到的另一个SO帖子是; http://stackoverflow.com/questions/4755505/how-to-recogized-touch-event-using-jquery-for-ipad-safari-browser-is-it-possible – Pat 2011-04-26 21:20:09