如何用Javascript/jQuery实现双指拖动?

3
我正在尝试创建一个能够在两个手指放在div上时拖动它的功能。
我已经将div绑定到了touchstart和touchmove事件,但是不确定如何编写这些函数。
类似于if event.originalEvent.targetTouches.length => 2设置开始的X和Y。
我需要平均两个手指的位置吗?然后使用数据应用css变换?如何使其脱离DOM流,静态定位?
如果有任何示例,那就太好了,谢谢!
1个回答

1
在 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接