我如何用 javascript/jquery 进行两指拖动?

2024-05-14

我正在尝试创建当有两个手指放在 div 上时拖动 div 的功能。

我已将 div 绑定到 touchstart 和 touchmove 事件。我只是不确定如何编写这些函数。

就像是if event.originalEvent.targetTouches.length => 2设置起始 X 和 Y。

我是否平均两个手指的位置?然后对数据应用 css 转换?如何将其从 DOM 流中拉出来,进行静态定位?

任何例子都会很棒,谢谢!


在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我如何用 javascript/jquery 进行两指拖动? 的相关文章

随机推荐