CSS悬停效果

时间:2013-03-07 01:27:56

标签: html css hover effects

我正在创建一个网站,并希望通过加载我在Photoshop中创建的颜色略有不同的另一个图像/按钮,在我的按钮上添加悬停效果。

当我应用代码时,没有任何事情发生。

这是我的代码:

CSS:

#main{
    position: relative;
    background-color:#ececec;
    height:900px;
    margin-top: 10px;
    margin-left: 10px;
    margin-right: 10px;

}


    #main .button2{

        position: absolute;
        left:0.5%;
        top: 71.5%;
        z-index:20;
        width:170px;
        height:40px;
    }
    #main .button2:hover{
        background-image : url(images/buttBootsRollover.png);  

    }

HTML:

<div id="main">
<article>


<section>
    <a href="#index.php" > <img src="images/buttGloves.png" class="button" /></a>
    <a href="#index.php" > <img src="images/buttBoots.png" class="button2" /></a>
    <a href="#index.php" > <img src="images/buttEqu.png" class="button3" /></a>
</section>

</article>

以下是可能提供更好概述的图片: enter image description here

最终我想在所有9个按钮上添加相同的悬停效果

4 个答案:

答案 0 :(得分:0)

你这样做的方式有点偏。您在包含图像的元素上设置background-image属性,该图像(可能)占据了元素的整个空间。你试图解决这个问题会让自己头疼,所以不要解释为什么你会得到你的结果,我只是告诉你不要这样做并给你一个解决方案:< / p>

摆脱div元素中的img元素。将button2类的background-image属性更改为您想要的默认图像。保留你的.button2:hover属性。

答案 1 :(得分:0)

这就是你要找的东西。但我相信你至少没有尝试过Google。我建议你先学习一个简单的CSS教程。

#main{
    background-color:#ececec;
    height:500px;
    width: 600px;
    margin:0px auto;
}
.button{               
        z-index:1000;
        width:170px;
        height:40px;        
        display:block;
        background-repeat:no-repeat;
}  
#but_one{
    background-image : url(images/but_one.png);
} 
#but_two{
    background-image : url(images/but_two.png);
} 
#but_three{
    background-image : url(images/but_three.png);
} 
#but_one:hover{
    background-image : url(images/but_one_h.png);
} 
#but_two:hover{
    background-image : url(images/but_two_h.png);
} 
#but_three:hover{
    background-image : url(images/but_three_h.png);
} 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="main.css" />
        <title>hi css</title>
    </head>
    <body>
    <div id="main">
    <a href="index.php" ><img id="but_one" class="button" /></a>
    <a href="index.php" ><img id="but_two" class="button" /></a>
    <a href="index.php" ><img id="but_three" class="button" /></a>
    <div>
    </body>
 </html>

答案 2 :(得分:0)

也许要实现你想要的你需要jquery

的帮助
<div class="box">
    <a href="#index.php" > <img src="images/buttGloves.png" class="button" /></a>
    <div class="overlay">play me!</div>
</div>

<div class="box">
    <a href="#index.php" > <img src="images/buttBoots.png" class="button2" /></a>
<div class="overlay">play me!</div>
</div>

<div class="box">
    <a href="#index.php" > <img src="images/buttEqu.png" class="button3" /></a>
<div class="overlay">play me!</div>
</div>

jquery的

$(function(){
    $(".box").hover(function(){
      $(this).find(".overlay").fadeIn();
    }
                    ,function(){
                        $(this).find(".overlay").fadeOut();
                    }
                   );        
});

工作demo

答案 3 :(得分:0)

只需拥有一张背景图片并隐藏一点。

例如 - 如此处所示,加倍http://jsfiddle.net/edheal/Qb7YG

这是代码

CSS:

#wibble
{
    background-image: url(http://well-spun.co.uk/portfolio/technologies/images/rollover.png);
    background-position: left top;
    background-repeat: no-repeat;
    height: 14px;
    width: 200px;
}

#wibble:hover
{
    background-position: left -14px;
}

HTML:

<div id="wibble">
</div>

图像高28px - 根据是否悬停显示上半部分或下半部分。

相关问题