如果你不介意你的悬停区域是“方”,然后使用纯CSS这应该工作(注意,你适当的图像更换背景颜色和对a
的border
是只是为了说明)。经测试在Firefox,IE7,IE8:
HTML:
<a href="#"><span class="img"></span></a>
CSS (编辑以IE7-8 BUGS):
body {
margin: 300px 218px 178px 400px; /*see explanation below css*/
width: 22px; /*total width of a tag including padding and borders*/
height: 22px; /*total height of a tag including padding and borders*/
}
a { /*warning, do not give this position: use margin to position it*/
width: 20px;
height: 20px;
display: block;
border: 1px solid red;
overflow: visible;
/*deleted margin from this: moved to body*/
}
a span.img {
position: absolute; /*this gives it block display by default*/
top: 0;
left: 0;
z-index: -1;
background-color: yellow; /*bw image here*/
width: 640px; /*image width*/
height: 500px; /*image height*/
}
a:hover span.img {
background-color: blue; /*color image here*/
}
/*deleted the a:hover span.img:hover as it was not needed after all*/
当然,如果IE6是一个问题,那么你需要用JavaScript来做点什么来识别span:hover
。
添加于编辑:我发现a
标签有时会悬停在IE浏览器定义区域之外。为了避免这种情况,身体必须边缘放置在使得left
top
和位置a
标记和right
和bottom
必须弥补在图像大小减去a
标签的总宽度的差。
只是想让你知道我发现了一些需要调整CSS以调整效果才能正常工作的IE错误(我注意到IE会激活超出'a'标记大小的链接) 。看到我编辑的CSS的变化。 – ScottS 2010-07-26 13:47:21