Changing :hover to touch/click for mobile devices

后端 未结 9 1939
余生分开走
余生分开走 2020-11-28 19:43

I\'ve had a look around but can\'t quite find what i\'m looking for.

I currently have a css animation on my page which is triggered by :hover. I would like this to c

相关标签:
9条回答
  • 2020-11-28 20:13

    Well I agree with above answers but still there can be an another way to do this and it is by using media queries.

    Suppose this is what you want to do :

    body.nontouch nav a:hover {
        background: yellow;
    }
    

    then you can do this by media query as :

    @media(hover: hover) and (pointer: fine) {
        nav a:hover {
            background: yellow;
        }
    }
    

    And for more details you can visit this page.

    0 讨论(0)
  • 2020-11-28 20:14

    I got the same trouble, in mobile device with Microsoft's Edge browser. I can solve the problem with: aria-haspopup="true". It need to add to the div and the :hover, :active, :focus for the other mobile browsers.

    Example html:

    <div class="left_bar" aria-haspopup="true">
    

    CSS:

    .left_bar:hover, .left_bar:focus, .left_bar:active{
        left: 0%;
        }
    
    0 讨论(0)
  • 2020-11-28 20:19
    document.addEventListener("touchstart", function() {}, true);
    

    This snippet will enable hover effects for touchscreens

    0 讨论(0)
  • 2020-11-28 20:19

    On most devices, the other answers work. For me, to ensure it worked on every device (in react) I had to wrap it in an anchor tag <a> and add the following: :hover, :focus, :active (in that order), as well as role="button" and tabIndex="0".

    0 讨论(0)
  • 2020-11-28 20:20

    If you use :active selector in combination with :hover you can achieve this according to w3schools as long as the :active selector is called after the :hover selector.

     .info-slide:hover, .info-slide:active{
       height:300px;
     }
    

    You'd have to test the FIDDLE in a mobile environment. I can't at the moment.
    correction - I just tested in a mobile, it works fine

    0 讨论(0)
  • 2020-11-28 20:26

    I think this simple method can achieve this goal. With CSS you can turn off pointer event to 'none' then use jQuery to switch classes.

    .item{
       pointer-events:none;
     }
    
    .item.clicked{
       pointer-events:inherit;
     }
     
    .item:hover,.item:active{
      /* Your Style On Hover Converted to Tap*/
      background:#000;
    }

    Use jQuery to switch classed:

    jQuery('.item').click(function(e){
      e.preventDefault();
      $(this).addClass('clicked')l
    });

    0 讨论(0)
提交回复
热议问题