css: avoid image hover first time blinking

前端 未结 12 620
深忆病人
深忆病人 2020-12-07 19:52

I have an anchor that changes its background image when hovered with a class class-btn that contains a background-image.

When hovered, it

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 20:27

    The easiest way to avoid this is to make use of image sprites. For a good overview, check out this CSS Tricks article.

    That way, you not only solve the flicker problem you're seeing, but will also reduce the number of HTTP requests. Your CSS will look something like:

    a.class-btn { background: url('path/to/image.jpg') 0 0 no-repeat; }
    a.class-btn:hover { background-position: 0 -40px; }
    

    The specifics will depend on your images. You can also make use of an online sprite generator to make the process easier.

提交回复
热议问题