Touch device, Single and Double Tap Events handler jQuery/Javascript?

后端 未结 4 1558
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 00:50

Background:

My site is in magento open source ecommerce solution, and in the top (header) it has a shopping cart icon. On dekstop, when user hovers

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 01:43

    This is the final working code that did the magic for me :) I have just copied and paste all the code in here but one may use it in accordance to own needs.

       if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
    
                $('#dropdown_menu').css({ "right":'20%'});
                var action;
                $('#continue_shopping').show();
                $('#continue_shopping').bind('touchstart click', function(event){
                    setTimeout(function(e){
                        $('#mini-cart').removeClass('open');
    
                        $('#dropdown_menu').hide();
                    }, 1200, [event]);
    
                }); 
    
                $('#dropdown-toggle').bind('touchstart', function(event){
                    var now = new Date().getTime();
                    var lastTouch = $(this).data('lastTouch') || now + 1 ;
                    var delta = now - lastTouch;
                    clearTimeout(action);
                    if(delta<500 && delta>0){
                        window.location='getUrl('checkout/cart');?>';
                    }else{
                        //if(delta == -1){
                        $('#mini-cart').addClass('open');
                        $('#dropdown_menu').show();
    
                        $(this).data('lastTouch', now);
                        action = setTimeout(function(e){
                            clearTimeout(action);  
                        }, 500, [event]);
                    }
                    $(this).data('lastTouch', now);
                 });
    
                if (typeof document.body.ontouchstart == "undefined") {
                    $('#dropdown-toggle').bind('click', function(event){
                        var now = new Date().getTime();
                        var lastTouch = $(this).data('lastTouch') || now + 1 ;
                        var delta = now - lastTouch;
                        clearTimeout(action);
                        if(delta<600 && delta>0){
                            window.location='getUrl('checkout/cart');?>';
                        }else{
                            //if(delta == -1){
                            $('#mini-cart').addClass('open');
                            $('#dropdown_menu').show(); 
    
                            $(this).data('lastTouch', now);
                            action = setTimeout(function(e){
                                clearTimeout(action);  
                            }, 600, [event]);
                        }
                        $(this).data('lastTouch', now);
                     });
                }
            }
    

提交回复
热议问题