Hover and click event on mobile devices

前端 未结 3 1438
滥情空心
滥情空心 2020-12-08 23:00

I am creating a responsive website for both desktop and mobile. I have one issue with a hover and click event that I am not sure how to solve for users on mobile devices.

相关标签:
3条回答
  • 2020-12-08 23:34

    I agree with @DZittersteyn on the fact that this is a bad design. You can better show the content by default in mobile so that the one who clicks knows what he clicked.

    if(!!('ontouchstart' in window)){//check for touch device
      $('myElement').unbind('click mouseenter mouseleave'); //use off if you used on, to unbind usual listeners
      $('myElement').on('click',function(){
        //slide down code
        setTimeout(function(){
           window.location.href='asdasd.html';
           },2000);
        });
    }
    

    or you can use

    if(!!('ontouchstart' in window)){//check for touch device
    //behaviour and events for touch device
    }
    else{
    //behaviour and events for pointing device like mouse
    }
    
    0 讨论(0)
  • 2020-12-08 23:47

    Try using jQuery to listen to the touchstart and touchend events for mobile.

    EX:

    $('selector').bind('touchstart', function(){
      //some action
    });
    $('selector').bind('touchend', function(){
      //set timeout and action
    });
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-09 00:01
    if (('ontouchstart' in window || (window.DocumentTouch && document instanceof DocumentTouch))) {
        $(".touch")
            .bind("touchstart", function() {
                $(this)
                    .addClass("active")
                    .bind("touchend", function() {
                        $(this).removeClass("active");
                    });
            })
            .bind("touchenter", function() {
                $(this)
                    .addClass("hover")
                    .bind("touchleave", function() {
                        $(this).removeClass("hover active");
                    });
            });
    }
    
    0 讨论(0)
提交回复
热议问题