开发者

CSS: Hover State images cache

开发者 https://www.devze.com 2023-01-28 17:29 出处:网络
开发者_运维百科when we define hover state of anything CSS... & on hover state we change the background:url(\'image path\'); will this image be preloaded of will be download on the first hover of t
开发者_运维百科

when we define hover state of anything CSS... & on hover state we change the background:url('image path'); will this image be preloaded of will be download on the first hover of the element.. if that is so then how to preload it... i know the javascript to preload images.. is that going to work...


If you're trying to avoid having your hover state images only load they they're being hovered on, instead of preloading them, why not create sprites that hold both the normal and hover images ? That way you can be sure that all your hover state images will already be loaded, and at the same time drop the overhead for all the requests. The only thing that you would then need to do, is to set the value of the background-position attribute.

As for your first question, I suppose the best way of finding the answer is to use two large images (a couple of wallpapers would work) and test it yourself, although I suspect that the images will only be loaded when the mouse is over the original image, because that's when the code is being executed.

Hope this helps !


If you have a div of height 20px, say, and want a background image to change on hover, use an image with both the no-hover and hover graphics in it, with the no-hover at the top, and the hover image at the bottom. Both parts should be the hight of your div, in this case, 20px. Then, set your CSS background-position first to 0px 0px (top left). This is default (no hover).

When the user hovers over the div, set the background-position to 0px -20px (20px up). This will move the background image up by 20px, showing the bottom half of the sprite, which is the hover graphic. When the mouse is removed from the div, the sprite will fall back to it's original position.

CSS:

.hoverDiv          /* Normal state */
{
    background: url('images/img.png');
    background-position: 0px 0px;
}

.hoverDiv:hover    /* Hover state */
{
    background-position: 0px -20px;     /* Move background up by 20px, hiding the top image */
}

If you have a div of different height, just change the 20px bits with the height of the div.

If your sprites are side by side as opposed to on top of each other, move the X axis by using background-position: -20px 0px; instead of 0px -20px;. Of course, you can move the background positively too.

Hope this helps,

James


you could use css sprites


The best thing to do is use CSS Sprites. A sprite sheet is a large image with lots of images inside it, which will be used on your site. What's the benefit? Well, it means that only one http request is sent to download all of your images. Therefore, making the site load slightly faster.

It will really work well with a hover effect!

It's much easier to use plus simple code. Not like JavaScript, with messy horrible code. It's very easy to learn. Based around the position of the image in the sprite. Here's a useful tutorial, on Flowdev. Here's an example on W3Schools

0

精彩评论

暂无评论...
验证码 换一张
取 消