HTML 图像翻转 - 翻转前图像未完全加载?
我有一个图像(在左上角作为主页链接),我使用 CSS :hover 在鼠标悬停时更改图像。
问题是,第一次翻转图像时需要一些时间来加载图像。有一个临时空白区域,您会看到图像逐渐加载。大约需要一秒钟,但这很烦人。
如何解决此问题以使翻转无缝?有没有办法预加载图像?
I have an image (in the top left as a home link), and I use the CSS :hover to change the image upon rollover.
The problem is, the image takes time to load the first time I rollover it. There's a temporary blank space, and you see the image incrementally load. It takes about a second, but it's annoying.
How can I fix this so the rollover is seamless? Is there a way to preload the image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可以立即想到两个选项:
:hover
图像放置在页面其他位置的隐藏div
中。1、精灵:
对于 CSS Sprite,为“主页”链接提供一张背景图片,只需更改其在
:hover
上的位置:不过,这确实需要为具有 css-sprite 背景的元素设置高度。
2、隐藏预加载元素:
There's two options I can think of, off-hand:
:hover
image in a hiddendiv
elsewhere in the page.1, sprites:
For a CSS Sprite, have one background image for the 'home' link, and simply change its position on the
:hover
:This does require set heights for the elements with the css-sprite background, though.
2, hidden preloading elements:
1.答案:
使用 CSS Sprites
2.答案: 或者创建类似这样的内容:
以及 CSS:
1. Answer:
Use CSS Sprites
2. Answer: Or create something like this:
And the CSS:
我发现对于翻转非常有效的一种方法是使用 CSS 精灵,即使用包含图像的原始版本和翻转版本的图像。这样两个版本就会同时加载,并且在它们之间进行更改只需更改
background-position
样式即可。A method that I have found works really well for rollovers is using CSS sprites, i.e. using an image that contains both the original and rollover versions of the image. That way both versions load at the same time, and changing between them is just a matter of changing the
background-position
style.一种方法是使用相同的 URL 将不可见图像添加到您的页面。因此,通过将以下内容添加到文档正文的开头,实际上是在指示浏览器尽快加载该图像。
虽然此标签仍然是文档的一部分,但由于“display:none”设置,它不会显示甚至不会呈现。您甚至可以监听其加载事件,并在加载后将标签从 DOM 中完全删除,以将“垃圾”排除在文档之外。一旦图像加载到内存中,每次引用相同的 URL 时都会自动重用该图像,因此一旦将其他图像的源设置为相同的 URL,它将从内存中加载。
希望有帮助,
科比
One way to do it is to add an invisible image to your page, with the same URL. So by adding the following to the beginning of the documents body, you actually instruct the browser to load this image as soon as possible.
While this tag remains a part of your document, it is not shown or even rendered because of the "display:none" settings. You can even listen to its load event and remove the tag completely from the DOM once it is loaded, to keep the "garbage" out of the document. Once an image is loaded to memory, it is automatically reused every time you refer to the same URL, so once you set the source of the other image to the same URL, it will be loaded from memory.
Hope that helps,
Kobi
将图像放入 div 中,样式设置为
{ height: 0;溢出:隐藏; }
,这应该使浏览器预加载图像。Put the image in a div with a style set to
{ height: 0; overflow: hidden; }
, that should make the browser preload the image.