简体   繁体   English

css3背景转换首先闪烁:悬停

[英]css3 background transition flickers on first :hover

There are some buttons on this page (the small icons toward the bottom) that I am using some css transitions to change the background of. 此页面上有一些按钮(底部的小图标),我正在使用一些CSS过渡来更改其背景。 But there is a flicker the first time the icons are hovered over! 但是第一次将图标悬停时会出现闪烁! I've tried to implement the suggestions offered on this thread , but to no avail. 我尝试实现此线程上提供的建议,但无济于事。 Has anyone an idea on how I can make this work without the flicker? 有谁知道如何使我的工作无闪烁?

Thank you so much! 非常感谢!

Since no minimal testcase provided, I can suppose your images need to be preloaded, and transitions has nothing to do with the issue. 由于没有提供最小的测试用例,因此我可以假设您的图像需要预加载,并且过渡与该问题无关。

A background image can be preloaded by specifying it as background for normal (not hover) state of an element, and adding background-position with negative value so that background image is not visible in normal state. 可以通过将背景图像指定为元素的正常(非悬停)状态的background-position ,并添加具有负值的background-position以使背景图像在正常状态下不可见来预加载背景图像。

For example: 例如:

/* Image is supposed to have height less than 100px here. */
A {
    background: url(example.png) 0 -100px no-repeat;
}

A:hover {
    background-position: 0 0;
}

By the way, it's established practice to put images for both states (normal and hover) to one physical image file, and then just change background-position on hovering: 顺便说一句,将两种状态(正常和悬停)的图像放到一个物理图像文件中,然后在悬停时更改background-position是一种既定的做法:

/* "rollover-sprite.png" file contains images for both states side by side. */
A {
    background: url(rollover-sprite.png) no-repeat;
}

/* Width of image for each state is supposed to be 100px here
  (sprite will be ~200px wide). */
A:hover {
    background-position: -100px 0;
}

You need to preload the image that you are switching to, the "flicker" is the short delay between you hovering and the image actually loading. 您需要预加载要切换到的图像,“闪烁”是指悬停和实际加载图像之间的短暂延迟。 There are lots of ways to do this, are you using jQuery? 有很多方法可以做到这一点,您使用的是jQuery吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM