javascript - 独立的 jQuery "touch"方法?

标签 javascript jquery iphone ipad

因此,我希望实现我编写的插件的功能,以从具有触摸功能的互联网设备(例如 iPhone、iPad 或 Android)读取触摸“滑动”。

外面有东西吗?我并不是在寻找像 jQtouch 这样完整的东西,尽管我正在考虑对我需要的代码进行逆向工程。

关于解决这个问题的最佳方法有什么建议吗?代码片段已经可用?

附录:事后我意识到解决方案并不严格是 jQuery,因为我很确定没有任何内置方法来处理这个问题。我希望标准 Javascript 能在答案中找到自己。

最佳答案

(function($) {
$.fn.swipe = function(options) {
    // Default thresholds & swipe functions
    var defaults = {
        threshold: {
            x: 30,
            y: 10
        },
        swipeLeft: function() { alert('swiped left') },
        swipeRight: function() { alert('swiped right') },
        preventDefaultEvents: true
    };

    var options = $.extend(defaults, options);

    if (!this) return false;

    return this.each(function() {

        var me = $(this)

        // Private variables for each element
        var originalCoord = { x: 0, y: 0 }
        var finalCoord = { x: 0, y: 0 }

        // Screen touched, store the original coordinate
        function touchStart(event) {
            console.log('Starting swipe gesture...')
            originalCoord.x = event.targetTouches[0].pageX
            originalCoord.y = event.targetTouches[0].pageY
        }

        // Store coordinates as finger is swiping
        function touchMove(event) {
            if (defaults.preventDefaultEvents)
                event.preventDefault();
            finalCoord.x = event.targetTouches[0].pageX // Updated X,Y coordinates
            finalCoord.y = event.targetTouches[0].pageY
        }

        // Done Swiping
        // Swipe should only be on X axis, ignore if swipe on Y axis
        // Calculate if the swipe was left or right
        function touchEnd(event) {
            console.log('Ending swipe gesture...')
            var changeY = originalCoord.y - finalCoord.y
            if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) {
                changeX = originalCoord.x - finalCoord.x

                if(changeX > defaults.threshold.x) {
                    defaults.swipeLeft()
                }
                if(changeX < (defaults.threshold.x*-1)) {
                    defaults.swipeRight()
                }
            }
        }

        // Swipe was canceled
        function touchCancel(event) { 
            console.log('Canceling swipe gesture...')
        }

        // Add gestures to all swipable areas
        this.addEventListener("touchstart", touchStart, false);
        this.addEventListener("touchmove", touchMove, false);
        this.addEventListener("touchend", touchEnd, false);
        this.addEventListener("touchcancel", touchCancel, false);

    });
};
})(jQuery);


$('.swipe').swipe({
 swipeLeft: function() { $('#someDiv').fadeIn() },
 swipeRight: function() { $('#someDiv').fadeOut() },
})

这就是检测 iPhone 的方法

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
     if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "path to iphone page";
}

关于javascript - 独立的 jQuery "touch"方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2701139/

相关文章:

javascript - 为什么要用分号?

javascript - 搜索 XML 文件

iphone - 如何在导航 Controller 栏上叠加 View ?

ios - 使用 iCloud Apple ID 匿名登录应用程序

javascript - 是否有一个 <a> 标记的事件处理程序在页面加载时触发?

javascript - LESS Css 计算 js 变量失败?

javascript - Jquery添加多个div

jquery - 查看是否在 IE8 中选中了单选按钮或复选框

javascript - Bootstrap 搞乱了未定义的 div

iphone - 如何在 XCode 中屏蔽不需要的 "Dead Store"警告?