我最近在我的一个页面中添加了一个 js 触摸映射函数(在此处的某处,在 stackoverflow 中找到),这样我就可以使我的 Jquery 可拖动对象在 iOS Safari 上工作(如果没有此映射,拖放将无法工作,因为据我所知)
这是函数:
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;
}
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, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
一切都很好......除了当我关注输入或文本区域(特别是 Facebook 评论插件)并且 iOS 键盘出现时。
在我输入文本并单击输入上方的“发送”按钮后,该函数为模拟的鼠标事件提供了错误的 Y 坐标(我相信这是因为键盘改变了页面的高度),我最终点击了某处在该按钮上方。
输入模糊,但发送按钮没有被点击。好像这还不够可悲,页面上的另一个按钮(发送按钮上方的一些像素)被点击并链接到另一个页面。
我想我可以在输入成为焦点时禁用映射,但似乎根本无法访问那些 Facebook 评论插件元素。
感谢任何帮助,我已经为此工作了好几天......
最佳答案
您是否使用过 UIview 来在显示键盘时向上推送内容?
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
textfield.frame = CGRectMake(textfield.frame.origin.x, (textfield.frame.origin.y + 100.0), textfield.frame.size.width, textfield.frame.size.height);
[UIView commitAnimations];
}
它现在将它从原点移动 100,如果您的元素需要它,您可以更改它。您还必须添加代码以在完成输入文本后从 View 中移除键盘。
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
textfield.frame = CGRectMake(textfield.frame.origin.x, (textfield.frame.origin.y + 100.0), textfield.frame.size.width, textfield.frame.size.height);
[UIView commitAnimations];
关于javascript - 使用 Javascript 专注于移动设备的触摸映射和输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7893355/