Media query for devices supporting hover

后端 未结 6 1296
醉梦人生
醉梦人生 2020-12-08 04:24

I\'d like to provide separate behaviour for browsers supporting hover (e.g. desktop browsers) and ones which don\'t (e.g. touchscreen devices). Specifically I want to declar

6条回答
  •  暖寄归人
    2020-12-08 04:51

    From the specs:

    none

    Indicates that the primary pointing system can’t hover, or there is no pointing system. Examples include touchscreens and screens that use a drawing stylus.
    Pointing systems that can hover, but for which doing so is inconvenient and not part of the normal way they are used, also match this value.

    For example, a touchscreen where a long press is treated as hovering would match hover: none.

    If your browser (mobile/touch) support long-press to simulate hover, the usage of hover: none will not work. What you can do is just use a default value and override it (with default css precedence):

    body {
        background: red;
    }
    
    @media (hover: hover) {
      body {
        background: blue;
      }
    }
    

    Desktop will have blue background and mobile/touch will have red background

    Check the following example:
    https://jsfiddle.net/mcy60pvt/1/

    To check the long-press option of the mobile you can use this example:
    https://jsfiddle.net/mcy60pvt/3/

    In the above example the green block has :hover definition for both desktop and mobile, however for desktop the background will change to yellow and for mobile (with long-press) it will change to white.

    Here is the css for the last example:

    body {
        background: red;
    }
    div.do-hover {
      border: 1px solid black;
      width: 250px;
      height: 250px;
      background: green;
    }
    div.do-hover:hover {
      background: white;
    }
    
    @media (hover: hover) {
      body {
        background: blue;
      }
      div.do-hover:hover {
        background: yellow;
      }
    }
    

    In this example - browsers that don't support :hover will view the hover me box with green background, and while "hover" (touch/long-press) - the background will change to white.

    update

    As for the added pseudo code:

    .myelement {
        /* some styling #1 */
        /* note: no hover state here */
    }
    @media(hover: hover) {
        .myelement {
            /* some styling that override #1 styling in case this browser suppot the hover*/
        }
        .myelement:hover {
            /* what to do when hover */
        }
    }
    

提交回复
热议问题