简体   繁体   English

如何在悬停时更改背景图像,以适应我的跨度?

[英]How do I make the background image change on hover, for my span?

Suppose I have a span: 假设我有一个跨度:

<span class="myspan"></span>

.myspan{
background: url(image.png) top left no-repeat;
}

How do I make it so that when people hover my span, it shows "image_hover.png"? 如何使人们悬停我的跨度时显示“ image_hover.png”?

.myspan:hover{
background-image:url('image_hover.png');
}

But what you really would want to do is make one image with both states, natural state and hover state, one beside the other. 但是,您真正想做的是使一个图像同时具有自然状态和悬停状态这两种状态。 Then you would have: 然后您将拥有:

.myspan{
width: *the width of only one state. aka half the width of the whole image*
background:url('image.png') top left no-repeat;
}

.myspan:hover{
background-postion:right;
}

That way only one image is loaded and on hover, it just moves it left to right. 这样一来,只有一幅图像被加载,并且悬停时,它只是将图像从左向右移动。 Displaying only one half of the image. 仅显示图像的一半。

You can apply the :hover pseudo class to every element: 您可以将:hover伪类应用于每个元素:

.myspan:hover
{
    background-image: url(image_hover.png);
}

Internet Explorer 6 won't care … but who cares about IE 6? Internet Explorer 6不在乎…但是谁在乎IE 6? :) :)

Alex, 亚历克斯

You can try 你可以试试

.myspan:hover{background:url('image_hover.png') top left no-repeat;}

You can also combine image.png and image_hover.png into one png image and simply move the position on hover. 您还可以将image.png和image_hover.png合并为一个png图像,并只需将鼠标悬停位置移动即可。 Your png would look like this: http://www.missgoodytwoshoes.com/images/nav_shop.png 您的png如下所示: http : //www.missgoodytwoshoes.com/images/nav_shop.png

And your code would look like this: 您的代码将如下所示:

<span class="myspan"></span>

.myspan{
background: url(image.png) top left no-repeat;
height: 37px;
}
.myspan:hover
{
background-position:0 -37px;
}

Use the :hover pseudo class: 使用:hover伪类:

.myspace:hover
{
    backbround: url(image-over.png) top left no-repeat;
}

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

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