-2

我想在 上用另一个图像替换一个图像hover,但我很难实现它 - 悬停时似乎没有发生任何事情。

JS小提琴

<div id="container">
    <div class="content">
        <div class="left">
            <a href="#"><img src="http://dummyimage.com/307x349/000/fff" alt="tyler st salon home"></a>
        </div><!-- end left -->
        <div class="center">
            <a href="#" class="top"><img src="http://dummyimage.com/307x166/000/fff" alt="about tyler st salon" class="about_hover"></a>
            <a href="#" class="bottom"><img src="http://dummyimage.com/307x166/000/fff" alt="tyler st salon service" class="services_hover"></a>
        </div><!-- end center -->
        <div class="right">
            <a href="#" class="top"><img src="http://dummyimage.com/307x166/000/fff" alt="tyler st salon products" class="products_hover"></a>
            <a href="#" class="bottom"><img src="http://dummyimage.com/307x166/000/fff" alt="contact tyler st salon" class="contact_hover"></a>
        </div><!-- end right -->
    </div><!-- end content -->    

</div><!-- end container -->

html, body, #container, .content, .left, .right, .center {
    height: 100%;
}

#container {
    min-height: 349px;
}

#container {
    margin: 0 auto;
    width: 960px;
}

#container .content {
    margin-top: 115px;
    position: relative;
}

#container .content .left,
#container .content .center,
#container .content .right { 
    position: relative;
    width: 307px;
}

#container .content .left {
    float: left;
}

#container .content .center {
    float: left;
    padding-left: 19px;
}

#container .content .right {
    float: right;
}

#container .content .top,
#container .content .bottom {
    position: absolute;
}

#container .content .top {
    top: 0;
}

#container .content .bottom {
    bottom: 0;
}

#container .content .center .about_hover {
    width: 307px;
    height: 166px;
    background: url('img/about_hover.jpg') center center no-repeat;
}

#container .content .center .services_hover {
    width: 307px;
    height: 166px;
    background: url('img/services_hover.jpg') center center no-repeat;
}

#container .content .center .products_hover {
    width: 307px;
    height: 166px;
    background: url('img/products_hover.jpg') center center no-repeat;
}

#container .content .center .contact_hover {
    width: 307px;
    height: 166px;
    background: url('img/contact_hover.jpg') center center no-repeat;
}
4

4 回答 4

2

这是 1 个变体
检查这个小提琴
当我悬停时隐藏你的图像

a:hover img
{
    display:none;
}

我将您的背景图片(我将您的班级从图片中移出)设置为<a>标签。

<a href="#" class="top about_hover">

示例小提琴仅适用于您的<a class="top"...

随意玩

于 2013-04-12T22:20:56.557 回答
1

您的 css 指定更改图像所覆盖区域的背景。要实际更改图像,您必须对<img src="...">内容进行猴子处理,可能通过 javascript 中的悬停事件(或 mousein/mouseout)侦听器。

这个答案显示了如何在点击事件上更改图像。要适应悬停事件,只需将事件侦听器更改为.hover()

于 2013-04-12T22:15:45.807 回答
0

这是写它的正确方法:

.element:hover{
/* stuff */
}

这是错误的:

.element_hover{
    /* stuff */
    }
于 2013-04-12T22:17:59.353 回答
0

这是一个javascript图像交换的例子(没有jQuery)

<!doctype html>
<html>
<head>
<script>
function byId(e){return document.getElementById(e);}
window.addEventListener('load', myInit, false);

function myInit()
{
    byId('img1').addEventListener('mouseover', onMouseOverOut, false);
    byId('img1').addEventListener('mouseout', onMouseOverOut, false);
}

// swaps the hoverImg and src attributes of the image it's attached to as an event handler
function onMouseOverOut()
{
    var tmp = this.src;
    this.src = this.getAttribute('hoverImg');
    this.setAttribute('hoverImg', tmp);
}
</script>
<style>
#img1
{
    height: 256px;
    width: 256px;
}
</style>
</head>
<body>
    <img id='img1' hoverImg='http://www.gravatar.com/avatar/c89d764421e3857bf346733ff58d8d2a?s=128&d=identicon&r=PG' src='http://dummyimage.com/307x349/000/fff'/>
</body>
</html>
于 2013-04-12T22:29:48.997 回答