從我對您的問題的理解中,您已經嘗試將您上面介紹的代碼與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。
touchHandler如何連線? – blaster