使用jQuery更改图像? [英] change images using jQuery?
问题描述
我想根据您悬停的列表项更改图片。
I'm trying to make an image change depending on the list-item you hover on.
我有一个包含ul的容器。我有三个列表项(一个小的水平导航)。列表项位于容器的底部,在李的顶部留下空白空间。我想要li上面的空白空间根据我悬停在哪个列表项更改图像。
I have a container containing an ul. I have three list-items (a small horizontal navigation). The list items are sitting on the bottom of the container which leaves an empty space on top of the li's. I want the empty space above the li's to change images depending on which list-item I hover over. There will be 3 different images or maybe using css sprites.
有什么方法可以让jQuery发生这种情况吗?
Is there any way I can make this happen with jQuery?
推荐答案
HTML:
<div id="container">
<img id="yourImg" src="spacer.gif" width="..." height="...">
<ul id="yourUL">
<li data-imageswap="someimg.png">One</li>
<li data-imageswap="another.png">Two</li>
<li data-imageswap="andathird.png">Three</li>
</ul>
</div>
脚本:
$('li','#yourUL').hover(function(){
$('#yourImg').attr('src',this.getAttribute('data-imageswap'));
}
您可以使用指向您的图片的数据
属性(如 className
或 id
),
You could use something other than that data
attribute (like a className
or an id
) to point to your image, but that should be the gist of it.
在图片中插入透明的1x1 gif
即可开始使用或者,你可以从没有图像开始写入DOM,如果你想开始没有它。
Sticking a transparent, 1x1 gif
in your image to start will let you scale it to the width and height you need. Alternately, you could start with no image and write it into the DOM if you'd like to start out without it.
这篇关于使用jQuery更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!