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

标签 javascript jquery iphone css ios

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

我已将 div 绑定(bind)到 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

关于javascript - 我将如何使用 javascript/jquery 进行两指拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926605/

相关文章:

iphone - 动画/视频作为启动图像 ios

JavaScript RegExp 匹配 "%.*",但不匹配 "\%.*"

Javascript事件函数问题

javascript - 是否可以在渲染前在缓存中插入图像

jquery - 重新审视 jQuery 推子功能问题

iphone - GitHub-带有 Storyboard的 iOS 应用程序

javascript - 在 [] 括号上拆分字符串

javascript - Vue.js 代码无法在没有 CDN 的 html 上运行

javascript - 识别手动选择元素的最佳方法

iphone - 我怎样才能创建这个数据结构?