gpt4 book ai didi

javascript - 使用一些翻译事件的代码(包括代码)在移动设备上拖放,但正常事件不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:46 24 4
gpt4 key购买 nike

我使用以下代码允许在我的网站上进行触摸拖放。但是,虽然我的代码中具有拖放功能的部分可以工作,但在 iPhone 上,我无论如何都无法滚动或移动页面。我可以拖放,但常规的 iPhone 触摸功能消失了!

我在几个 Stack Overflow 响应中找到了以下代码片段,所以我想知道是否有解决方法。我希望能够在 Web 和移动设备上进行拖放操作。

我的拖动代码(只是一个列表):

$(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 的内容,并使用它们来填充将事件监听器仅绑定(bind)到 <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();
});
}

关于javascript - 使用一些翻译事件的代码(包括代码)在移动设备上拖放,但正常事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8058699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com