ホバー領域が「正方形」であることを気にしない場合は、純粋な css を使用するとうまくいくはずです (背景色を適切な画像に置き換えてください。border
上の画像a
は説明用です)。Firefox、IE7、IE8 でテスト済み:
HTML:
<a href="#"><span class="img"></span></a>
CSS (IE7-8 バグ用に編集) :
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