1

当您将光标放在原始图像上时,我无法创建不同的图像。这是我的代码,没有我列出的其余按钮:

<nav class="buttons">
<ul>
    <li class="left">
    <a class="home" href="www.google.com">
    <img src="img/Home_2.png"></a></li>
</ul>
</nav>

CSS:

.buttons img{width: 190px; margin:0px; padding:0px; margin:0 auto; margin-top:55px;}
.buttons ul{list-style-type: none; margin:0px; padding:0px}

.left{float:left}
.home:hover {background: url(../img/Home_crack.PNG)}

有什么建议么?


编辑:好的很好的建议,但是当我将鼠标悬停在“主页按钮”上时,浮动中的东西会消失。这是具有完整浮点属性的代码:

HTML:

<nav class="buttons">
<ul>
    <li class="left">
    <a class="home" href="www.google.com">
    <img src="img/Home_2.png"></a></li>

    <li class="left">
    <a href="www.google.com" class="menu">
    <img src="img/Menu_2.png"></a></li>

    <li class="right">
    <a href="www.google.com" class="about">
    <img src="img/About_2.png"></a></li>

    <li class="right">
    <a href="www.google.com" class="contact">
    <img src="img/Contact_2.png"></a></li>
</ul>
</nav>

CSS:

.buttons img{width: 190px; margin:0px; padding:0px; margin:0 auto; margin-top:55px;}
.buttons ul{list-style-type: none; margin:0px; padding:0px}

.left{float:left}
.home:hover img{display:none} 
.home:hover {background: url(../img/Home_crack.PNG);}

.right{float:right}

基本上我想分开 4 个按钮....一个在左浮动,一个在右浮动,然后在悬停时,按钮会变成不同的图像....使用新的 img{display:none}左浮动快速闪烁。

4

4 回答 4

2

如果你想要一个不同img的 on :hover,你可以通过:afterpsuedo 元素添加它background-image

这种方法适用于动态img维度。

jsFiddle 这里- 不同img将显示:hovera.

a:hover:after {
    content: "\A";
    position: absolute;
    z-index: 99;
    width: 100%;
    height: 100%;
    background: url('http://...');
    top: 0px;
    left: 0px;
}
于 2013-10-21T21:41:25.050 回答
0

除了这里的其他人都说要将它添加到 CSS 中:

.home:hover img { display: none }

您还需要为要放置背景的 a 标签添加高度和宽度,否则标签中将没有内容并且根本不会显示。

.home:hover {
   background: url(../img/Home_crack.PNG)
   width: [enter the width of the bg image];
   height: [enter the height of the bg image];
}
于 2013-10-21T21:39:41.210 回答
0

您正在更改标签的背景,该<a>标签恰好被<img>. 因此,背景实际上是不可见的,因为 img 完全覆盖了它。尝试放入一个

.home:hover img { display: none }

规则以及在悬停时隐藏 img。这将允许背景“显示”。

于 2013-10-21T21:34:24.963 回答
0

尝试添加以下行,这将在悬停链接时隐藏原始图像:

.home:hover img {
    display: none;
}

我认为正在显示另一张图像,但默认图像不会消失,只是在悬停图像之前。

现在,我还建议您将a元素的显示样式更改为block并给它一个固定大小,因为a隐藏图像元素时元素的大小将为 0:

.home {
    display: block;
    width: 100px;
    height: 100px;
}

最后,我建议您在元素中也使用默认图像作为背景a,以确保它与悬停图像完全相同。现在您正在使用一个img元素来显示默认图像,另一个元素来显示悬停图像。我建议您防止这种情况发生,并尝试使用一个元素。只需添加此行即可实现此目的:

.home:hover {
    background: url(../img/Home_2.png)
}

希望这可以帮助!

于 2013-10-21T21:34:35.243 回答