在CoffeeScript中,我也編輯它來使用全局變量來進行泛化。我沒有使用全局變量。
$('#div').bind 'touchstart', touchstart
$('#div').bind 'touchmove', touchmove
touchstart: (event) =>
if event.originalEvent.touches.length >= 2
x = 0
y = 0
for touch in event.originalEvent.touches
x += touch.screenX
y += touch.screenY
window.startx = x/event.originalEvent.touches.length
window.starty = y/event.originalEvent.touches.length
touchmove: (event) =>
if event.originalEvent.touches.length >= 2
x = 0
y = 0
for touch in event.originalEvent.touches
x += touch.screenX
y += touch.screenY
movex = (x/event.originalEvent.touches.length) - @startx
movey = (y/event.originalEvent.touches.length) - @starty
newx = $('#div').offset().left + movex
newy = $('#div').offset().top + movey
$('#div').offset({top: newy, left: newx})
window.startx = x/event.originalEvent.touches.length
window.starty = y/event.originalEvent.touches.length