我想阻止滚动页面“离开 iPhone 屏幕”(当页面边框后面的灰色 Safari 背景可见时)。为此,我取消 touchmove 事件:
// Disables scrolling the page out of the screen.
function DisableTouchScrolling()
{
document.addEventListener("touchmove", function TouchHandler(e) { e.preventDefault(); }, true);
}
不幸的是,这也会禁用 mousemove 事件:当我点击按钮然后将手指移出按钮,然后释放屏幕时,按钮的 onclick 事件无论如何都会被触发。
我尝试过将触摸事件映射到鼠标事件上,如下所述:http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/ ,但无济于事(相同的行为)。
有什么想法吗?
最佳答案
根据我对您问题的理解,您尝试将上面提供的代码与 the code snippet provided by Ross Boucher on Posterous 结合起来。 。尝试将这两个片段连续组合是行不通的,因为在禁用 touchmove
时,您还禁用了允许 mousemove
通过他的示例工作的垫片.
This question and its answers为你的问题制定一个可行的解决方案。您应该尝试这两个代码片段,看看它们是否可以解决您的问题:
This snippet,这会禁用旧的滚动行为:
elementYouWantToScroll.ontouchmove = function(e) {
e.stopPropagation();
};
或者这个,来自 the same :
document.ontouchmove = function(e) {
var target = e.currentTarget;
while(target) {
if(checkIfElementShouldScroll(target))
return;
target = target.parentNode;
}
e.preventDefault();
};
然后,请访问the code on Posterous :
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();
}
这应该适合你。如果没有,则表明其他功能无法与 Mobile Safari 配合使用。
关于iphone - 移动Safari : Disable scrolling pages "out of screen",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949049/