iPad/iPhone hover problem causes the user to double click a link

后端 未结 26 1706
太阳男子
太阳男子 2020-11-27 09:18

I have some websites I built times ago, that use jquery mouse events...I just got an ipad and i noticed that all the mouse over events are translated in clicks...so for inst

26条回答
  •  一生所求
    2020-11-27 09:46

    Just an improvement to avoid redirection when you slide your finger on a link by mistake.

    // tablet "one touch (click)" X "hover" > link redirection
    $('a').on('touchmove touchend', function(e) {
    
        // if touchmove>touchend, set the data() for this element to true. then leave touchmove & let touchend fail(data=true) redirection
        if (e.type == 'touchmove') {
            $.data(this, "touchmove_cancel_redirection", true );
            return;
        }
    
        // if it's a simple touchend, data() for this element doesn't exist.
        if (e.type == 'touchend' && !$.data(this, "touchmove_cancel_redirection")) {
            var el = $(this);
            var link = el.attr('href');
            window.location = link;
        }
    
        // if touchmove>touchend, to be redirected on a future simple touchend for this element
        $.data(this, "touchmove_cancel_redirection", false );
    });
    

提交回复
热议问题