在移动设备上拖放使用一些代码来翻译事件(包括代码),但正常事件不起作用
Drag drop on mobile devices using some code that translates event (code included), but normal events are not working
我使用下面的代码来允许在我的网站上触摸拖放。然而,虽然我的代码部分具有拖放功能,但在iPhone上,我无法滚动或移动页面。我可以拖动&但是普通的iPhone触摸功能已经消失了!
我在几个堆栈溢出响应中发现了下面的代码片段,所以我想知道是否有解决这个问题的方法。我希望能够拖动&在网页和手机上都可以点击。
我的拖拽代码(只是一个列表):
$(function() {
$(".drag_me ul").sortable();
});
触摸事件代码:
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;
}
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);
}
我一直在研究同样的问题,我想我刚刚成功了。我删除了touchHandler的核心,并使用它们来填充回调,该回调仅将事件侦听器绑定到<div id='stage'>
的子节点。下面是我使用jquery的代码。希望这对你有帮助!
function init(){
$("#stage").children().bind('touchstart touchmove touchend touchcancel', function(){
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;
}
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();
});
}
这行代码阻止了默认的滚动功能:
event.preventDefault();
由于此事件对象与文档绑定,因此所有本机卷轴都被禁用。
如果你想允许本地滚动页面,那么将事件侦听器添加到document元素的后代(这样,如果用户在可排序容器以外的区域滚动,他们将能够正常滚动页面)。
改变:
function init()
{
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
:
function init()
{
$('#sortable_container').bind('touchstart touchmove touchend touchcancel', touchHandler);
}
相关文章:
- 如何将javascript事件从web浏览器wpf控件发送到wpf的c#代码
- php代码在textbox更改事件上显示null
- 如何在Win8Metro应用程序的Javascript代码中捕获自己的C#事件
- 访问代码生成的输入元素上的keyup事件
- 如何在 JavaScript 代码中调试点击事件处理
- 如何使用我的Jquery代码创建委托事件侦听器
- 如何使用onclick事件触发此代码.我尝试过,但失败了
- 在单击事件中,第一次单击时激发代码一次,其余代码在所有单击时运行
- 为什么动态加载的事件在我的代码中不能正常工作
- 我需要java代码来打开html代码onclick事件
- 使用Javascript代码在文本框上激发退格事件
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- 如何在onclick事件执行代码时在ImageButton上设置加载gif
- 在按钮'上运行PHP代码;s onclick事件,或者使用Ajax
- 我想在每个按钮点击事件上加载多个图表.我使用了下面的谷歌代码
- HTML5 嵌入代码 - 事件属性未触发(已触发、暂停等)
- 在ionicPopup完成加载后运行代码/事件
- 如何在添加键代码事件处理程序后删除它
- 调试JS/CoffeeScript代码:事件,回调等
- 当值更改时,用于表单文本字段的代码事件侦听器