本文介绍了javascript/jquery中的触控板点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以处理来自Mac触控板的敲击吗?

Is there any way to handle tap from track pad of mac?

我需要在触控板上(尤其是在Mac上)处理轻按"和单击".

I need to handle 'tap' and 'click' on track-pad, especially on mac.

我尝试了

$.event.special.tap = {
    setup: function(data, namespaces) {
        var $elem = $(this);
        $elem.bind('touchstart', $.event.special.tap.handler)
             .bind('touchmove', $.event.special.tap.handler)
             .bind('touchend', $.event.special.tap.handler);
    },

    teardown: function(namespaces) {
        var $elem = $(this);
        $elem.unbind('touchstart', $.event.special.tap.handler)
             .unbind('touchmove', $.event.special.tap.handler)
             .unbind('touchend', $.event.special.tap.handler);
    },

    handler: function(event) {
        event.preventDefault();
        var $elem = $(this);
        $elem.data(event.type, 1);
        if (event.type === 'touchend' && !$elem.data('touchmove')) {
            event.type = 'tap';
            $.event.handle.apply(this, arguments);
        } else if ($elem.data('touchend')) {
            $elem.removeData('touchstart touchmove touchend');
        }
    }
};

$('.thumb img').bind('tap', function() {
    //bind tap event to an img tag with the class thumb
}

哪个没用.

如何在触控板上捕获点击事件?

How to capture tap event on the track-pad?

推荐答案

我也遇到了同样的问题.看起来,例如MacBook上的触控板不会像实际的触摸设备那样触发TouchEvent.相反,它是将手势转换为MouseEvents.

I'm having this same concern. It appears that the trackpad on the MacBook (for example) doesn't fire TouchEvents like an actual touch device would. It is instead converting gestures into MouseEvents.

document.addEventListener('mousewheel', function(e) {
    console.log(e.wheelDelta);
});

document.addEventListener('touchstart', function(e) {
    console.log(e);
});

在上面的示例中,当我尝试在触控板上捕获捏/缩放"手势时,实际上是从滚轮上恢复了Delta,就像我按住CTRL然后向上或向下滚动一样,返回wheelDelta值120或-120.将事件侦听器用于"touchstart"不会像我所规定的那样写入控制台,除非它位于平板电脑或智能手机之类的触摸设备上.

In the above example, when I attempt to capture the "pinch/zoom" gesture on a trackpad, I'm actually getting back Delta from a scroll wheel as if I'd held down CTRL and then scrolled up or down, returning a wheelDelta value of 120 or -120. Putting event listeners on for 'touchstart' doesn't not write to the console as I prescribe unless it's on a touch device like a tablet or smart phone.

很明显,MackBook可以检测到您何时触摸了触控板,因为它可以检测到您的移动,但是似乎无法在文档中找到.

Obviously, the MackBook can detect when you've touched the trackpad since it's able to then detect your movement, but it does not appear to be available through the document.

这篇关于javascript/jquery中的触控板点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:16