简体   繁体   English

HTML图像翻转-翻转之前图像未完全加载?

[英]HTML Image Rollover - Image isn't fully loaded before rollover?

I have an image (in the top left as a home link), and I use the CSS :hover to change the image upon rollover. 我有一个图像(在左上角为主页链接),并且我使用CSS:hover在翻转时更改了图像。

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? 有没有办法预加载图像?

There's two options I can think of, off-hand: 我可以临时考虑两种选择:

  1. css image sprites, or CSS图像精灵,或
  2. placing the :hover image in a hidden div elsewhere in the page. :hover图片放置在页面其他位置的隐藏div中。

1, sprites: 1,精灵:

For a CSS Sprite , have one background image for the 'home' link, and simply change its position on the :hover : 对于CSS Sprite ,为“ home”链接提供一个背景图片,只需更改其在:hover上的位置即可:

#home {
    background-image: url(http://example.com/spriteOne.png);
    background-repeat: no-repeat;
    background-position: 0 100px;
}

#home:hover {
    background-position: 0 200px;
}

This does require set heights for the elements with the css-sprite background, though. 但是,这确实需要为具有css-sprite背景的元素设置高度。


2, hidden preloading elements: 2,隐藏的预加载元素:

#home {
    background-image: url(http://example.com/bg.png);
    background-repeat: no-repeat;
    background-position: 0 100px;
}
#home:hover {
    background-image: url(http://example.com/hoverBg.png);
}
#preload,
#preload img {
    display: none;
}

<div id="preload">
    <img src="http://example.com/hoverBg.png" />
</div>

A method that I have found works really well for rollovers is using CSS sprites, ie using an image that contains both the original and rollover versions of the image. 我发现一种非常适合翻转的方法是使用CSS Sprite,即使用包含图像原始版本和翻转版本的图像。 That way both versions load at the same time, and changing between them is just a matter of changing the background-position style. 这样,两个版本会同时加载,并且在它们之间进行更改只是更改background-position样式的问题。

One way to do it is to add an invisible image to your page, with the same URL. 一种方法是使用相同的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. 因此,通过在文档正文的开头添加以下内容,您实际上指示浏览器尽快加载此图像。

<IMG src="rolloverImage.png" style="display:none">

While this tag remains a part of your document, it is not shown or even rendered because of the "display:none" settings. 虽然此标记仍是文档的一部分,但由于设置了“ display:none”,因此无法显示甚至渲染。 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. 您甚至可以监听其load事件,并在DOM加载后将其完全从DOM中删除,以将“垃圾”拒之门外。 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. 将图像加载到内存后,每次您引用相同的URL时,它将自动重用,因此,将其他图像的源设置为相同的URL后,将从内存中加载该图像。

Hope that helps, Kobi 希望有帮助,科比

1. Answer: Use CSS Sprites 1.答案:使用CSS Sprites

2. Answer: Or create something like this: 2.答案:或创建如下内容:

<a href="link.html">

<!-- Remove this img-tag if you don't have a 'nohover' background -->
<img alt="image" src="images/image.png" class="nohover" />

<img alt="imagehover" src="images/image.png" class="hover" />

</a>

And the CSS: 而CSS:

img.nohover { img.nohover {

border:0 边框:0

} }

img.hover { img.hover {

border:0; 边界:0;

display:none 显示:无

} }

a:hover img.hover { a:hover img.hover {

display:inline 显示:内联

} }

a:hover img.nohover { a:hover img.nohover {

display:none 显示:无

} }

Put the image in a div with a style set to { height: 0; overflow: hidden; } 将图片放在div中,并将其样式设置为{ height: 0; overflow: hidden; } { height: 0; overflow: hidden; } { height: 0; overflow: hidden; } , that should make the browser preload the image. { height: 0; overflow: hidden; } ,这将使浏览器预加载图像。

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

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