How do I simulate a hover with a touch in touch enabled browsers?

后端 未结 14 2086
旧巷少年郎
旧巷少年郎 2020-11-22 17:04

With some HTML like this:

Some Text

Then some CSS like this:

p {
  color:black;
}

p:hover {
  color:red         


        
14条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 17:18

    My personal taste is to attribute the :hover styles to the :focus state as well, like:

    p {
        color: red;
    }
    
    p:hover, p:focus {
        color: blue;
    }
    

    Then with the following HTML:

    WOOOO

    And the following JavaScript:

    $("#some-p-tag").on("touchstart", function(e){
        e.preventDefault();
        var $elem = $(this);
    
        if($elem.is(":focus")) {
            //this can be registered as a "click" on a mobile device, as it's a double tap
            $elem.blur()
        }
        else {
            $elem.focus();
        }
    });
    

提交回复
热议问题