最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript: translate long tap events to right click events - Stack Overflow

programmeradmin3浏览0评论

How can one automatically translate long tap events to right click events? Since many touch devices like the iPad don't provide a way to do a right click on a website this would be very handy because a website's code doesn't need to be adjusted.

For example this code is designed for desktop browser having mouse support:

<html>
    <head><title>Long tap to right click test</title></head>
    <body>
        <img src="dummy.png" oncontextmenu="alert('Hi!'); return false;" width="20" height="20" />
    </body>
</html>

The goal is to translate a long tap event to the right click event without modifying the code. (Just loading some JavaScript, of course.)

If've seen that / does something similar for drag'n'drop support on jQuery widgets. However this plugin doesn't support the long tap.

Also / does actually perform the desired translation but it brakes scrolling, thus making it useless for regular websites with the need of scroll support.

Any help is appreciated - thanks!

How can one automatically translate long tap events to right click events? Since many touch devices like the iPad don't provide a way to do a right click on a website this would be very handy because a website's code doesn't need to be adjusted.

For example this code is designed for desktop browser having mouse support:

<html>
    <head><title>Long tap to right click test</title></head>
    <body>
        <img src="dummy.png" oncontextmenu="alert('Hi!'); return false;" width="20" height="20" />
    </body>
</html>

The goal is to translate a long tap event to the right click event without modifying the code. (Just loading some JavaScript, of course.)

If've seen that https://github./furf/jquery-ui-touch-punch/ does something similar for drag'n'drop support on jQuery widgets. However this plugin doesn't support the long tap.

Also http://code.google./p/jquery-ui-for-ipad-and-iphone/ does actually perform the desired translation but it brakes scrolling, thus making it useless for regular websites with the need of scroll support.

Any help is appreciated - thanks!

Share Improve this question asked Feb 14, 2013 at 13:50 jorjor 2,1554 gold badges27 silver badges46 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can write simple plugin to handle this type of events. Lets call it longTap event. Example:

$.fn.longTap = function(options) {

    options = $.extend({
        delay: 1000,
        onRelease: null
    }, options);

    var eventType = {
        mousedown: 'ontouchstart' in window ? 'touchstart' : 'mousedown',
        mouseup: 'ontouchend' in window ? 'touchend' : 'mouseup'
    };

    return this.each(function() {
        $(this).on(eventType.mousedown + '.longtap', function() {
            $(this).data('touchstart', +new Date);
        })
        .on(eventType.mouseup + '.longtap', function(e) {
            var now = +new Date,
                than = $(this).data('touchstart');
            now - than >= options.delay && options.onRelease && options.onRelease.call(this, e);
        });
    });
};

Obviously you want to change mousedown and mouseup to touchstart and touchend in case of iPad.

Usage: http://jsfiddle/dfsq/RZgxT/1/

You can use a timeout for that:

var timeoutLongTouch;
var $mydiv = $j('#myDiv');

// Listen to mousedown event
$mydiv.on('mousedown.LongTouch', function () {
    timeoutLongTouch = setTimeout(function () {
        $mydiv.trigger('contextmenu');
    }, 1000);
})
// Listen to mouseup event
.on('mouseup.LongTouch', function () {
    // Prevent long touch 
    clearTimeout(timeoutLongTouch);
});

All solutions not work in desktop browsers. You should also tune up 'click' handler behaviour, cause all 'longtap' events should also be followed by 'click' event.

In this case something code like this:

itemEl.click(function(event){

    if ($(this).data('itemlongtouch')){
        $(this).data('itemlongtouch', false);
    }else{
        //some work
    }
});

itemEl.longTap(function(event){
    $(this).data('itemlongtouch', true);
    //some work
});
发布评论

评论列表(0)

  1. 暂无评论