我想在hover
上用另一张图片替换图像,但是我很难实现它 - 在悬停时似乎没有任何事情发生。
<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;
}
答案 0 :(得分:2)
这是一个变体
检查此fiddle
我悬停时隐藏你的图像
a:hover img
{
display:none;
}
然后我将您的背景图片(我将您的课程从图片中移动)设置为<a>
标记。
<a href="#" class="top about_hover">
示例小提琴仅适用于您的<a class="top"...
随意玩它
答案 1 :(得分:1)
您的css指定更改图像覆盖区域的背景。要实际更改图像,您必须使用<img src="...">
内容,可能是通过javascript中的悬停事件(或mousein / mouseout)侦听器。
This answer显示了如何更改点击事件中的图片。要适应悬停事件,只需将事件监听器更改为.hover()
答案 2 :(得分:0)
这是写它的正确方法:
.element:hover{
/* stuff */
}
这是错误的:
.element_hover{
/* stuff */
}
答案 3 :(得分: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>