What I'm trying to achieve is basicly having navigation elements like
link1 link2 link3
and below them there's an image
[x] <- defaultImage
今私が欲しいのは、link1にカーソルを合わせると画像がimage1に変わり、link2にカーソルを合わせると画像がimage2に変わり、...そしてonMouseOutにすると画像がdefaultImageに戻ることです
私はこれを使っていました
function mouseOverImage(elm) {
var id = document.getElementById("catPic");
img.style.width = elm.getAttribute('data-cat-width');
img.style.height = elm.getAttribute('data-cat-height');
}
function mouseOutImage() {
var id = document.getElementById("catPic");
img.style.width = img.getAttribute('data-default-width');
img.style.height = img.getAttribute('data-default-height');
}
+
<ul>
<li>
<a href="link.htm" data-cat-image="images/img11.jpg"
data-cat-width="280px"
data-cat-height="336px" onmouseover="mouseOverImage(this)"
onmouseout="mouseOutImage()">link1</a>
</li>
<li><a href="link2.htm" data-cat-image="images/img22.jpg"
data-cat-width="280px"
data-cat-height="436px" onmouseover="mouseOverImage(this)"
onmouseout="mouseOutImage()">link2</a></li>
+
<div class="floatimg">
<img src="" width="0" height="0" alt="" id="catPic"
data-default-image="images/default.jpg"
data-default-width="0px" data-default-height="40px">
</div>
そしてそれはうまくいきます
しかし
今、私はアプローチを変更したいと思います。onMouseOver を介して変更を呼び出すたびに異なる画像を使用する代わりに、Spritesを使用したいと考えています。
そして、私はそれにどのようにアプローチすればよいか、本当に苦労していますか?
いくつかの画像から画像スプライトを生成し、次のようなコードを持っています
.sprite-1, .sprite-2, .sprite-3, .sprite-4,
{ display: block; background: url('tmp.png') no-repeat; }
.sprite-3 { background-position: -0px -0px; width: 280px; height: 441px; }
.sprite-4 { background-position: -288px -0px; width: 280px; height: 420px; }
.sprite-5 { background-position: -576px -0px; width: 280px; height: 420px; }
私はスクリプトについて何も知らないので、これを全くの初心者の質問として扱ってください。あなたが持っていて答えるなら、コードをどこに置くべきか教えてください
助けてくれてありがとう!