一個不好的但快速解決這個問題是將觸摸事件映射到您的里程錶上的鼠標。從https://stackoverflow.com/a/1781750/3946520
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 mapTouchToMouse(elem)
{
elem.addEventListener("touchstart", touchHandler, true);
elem.addEventListener("touchmove", touchHandler, true);
elem.addEventListener("touchend", touchHandler, true);
elem.addEventListener("touchcancel", touchHandler, true);
}
mapTouchToMouse(document.getElementById("p1-odometer"));
mapTouchToMouse(document.getElementById("p2-odometer"));
也採取使其邊緣和IE瀏覽器中添加的樣式屬性touch-action: none; -ms-touch-action:none;
到您的SVG元素:
<svg id="p2-odometer" width="100%" viewBox="-26 -26 57 52" background="#eee" style="touch-action:none; -ms-touch-action:none;">
<svg id="p1-odometer" width="100%" viewBox="-26 -26 57 52" background="#eee" style="touch-action:none; -ms-touch-action:none;">
你也不必加載觸摸衝jquery- ui擴展到該方法的頁面工作。
如果您不想使用此方法並希望通過自定義觸摸處理,那麼您需要使用這些事件:'touchstart', 'touchmove', 'touchend', 'touchcancel'
代替'mousedown', 'mousemove', 'mouseup', 'mouseleave'
。並對這些事件的處理函數進行相應的更改。我希望這有幫助。
這似乎已經完成了這項工作。 –