帮助多图像jquery悬停
大家好,抱歉我是 Jquery 新手。我一直尝试使用 CSS 完成所有工作,但我正在尝试大幅减少网站的代码。 我想做的是,当一个 div.class img 悬停时,它将同时更改 2 个图像的源文件。 我可以做到这一点,但我需要这两个图像具有不同的源文件。 我还试图保持代码足够简单,以便我可以在 HTML 中设置图像源,而不必在脚本中设置每个图像。原因是实际应用程序将有数百张图像。在 CSS 和脚本中定义翻转状态过于庞大和混乱。
这是我到目前为止所拥有的。
<script>
$(function() {
$(".on img").hover(
function() {
$(".on img").attr("src", $(".on img").attr("src").split(".").join("_hover."));
},
function() {
$(".on img").attr("src", $(".on img").attr("src").split("_hover.").join("."));
});
});
</script>
因此,我使用 DIV 类来指定我想要更改或将悬停的任何图像。 因此,当该类中的任何其他图像悬停时,任何图像都将具有该翻转状态。
现在的问题是,当该类中的所有图像发生变化时,它们都将更改为相同的图像源,而我需要它们更改为自己的图像源。因此,x.jpg 变为 x_hover.jpg,y.jpg 变为 y_hover.jpg,依此类推。
以下是本示例的 HTML so。
<div class="on"><img src="api.jpg" /> </div>
<div class="on"><img src="ex.jpg" /></div>
<div class="on"><img src="api.jpg" /> </div>
<div class="on"><img src="ex.jpg" /></div>
谢谢!!任何人都可以提供帮助,我们将不胜感激。我是 Jquery 的新手,我已经搜索了好几天如何执行此操作。
-JK
Hey guys, sorry I am new to Jquery. I have always tried to do everyting with CSS but I am trying to cut my code down significantly for a website.
What I am trying to do, is have it so that when one div.class img is hovered, that it will change the source file of 2 images at the same time.
I can get this to happen, but I need those two images to have different source files.
I am also trying to keep the code simple enough that I can set my image source in the HTML and not have to set every image in the script. The reason for this is the actual application will have several 100 images. Defining the rollover states in CSS and in the script is too bulky and messy.
Here is what I have so far.
<script>
$(function() {
$(".on img").hover(
function() {
$(".on img").attr("src", $(".on img").attr("src").split(".").join("_hover."));
},
function() {
$(".on img").attr("src", $(".on img").attr("src").split("_hover.").join("."));
});
});
</script>
So I am using a DIV class to specify any of the images that I want to change, or that will be hovered.
So any image will have that rollover state, when any other image in that class is hovered.
The problem is right now, that when all the images in that class change, they are all changing to the SAME image source where I need them to change to their own image source. So x.jpg becomes x_hover.jpg and y.jpg becomes y_hover.jpg, etc.
Here is the HTML so for this example.
<div class="on"><img src="api.jpg" /> </div>
<div class="on"><img src="ex.jpg" /></div>
<div class="on"><img src="api.jpg" /> </div>
<div class="on"><img src="ex.jpg" /></div>
Thanks!! Anyone that can help, it will be greatly appreciated. I am new to Jquery and I have been searching for days on how to do this.
-JK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的最简单的方法是更改悬停元素的 src :
JS Fiddle 演示。
Edited in response to question, in comments to this answer, from @Jamie:
以下 jQuery 脚本将为您处理该问题:
JS Fiddle 演示。
已编辑以修复上述 URL,由于某种原因,该 URL 是图像而不是演示。愚蠢的我......它现在链接到实际的演示。对不起。
The easiest way I could come up with for this, to change the
src
of the hovered-over element is:JS Fiddle demo.
Edited in response to question, in comments to this answer, from @Jamie:
The following jQuery script will take care of that for you:
JS Fiddle demo.
Edited to fix the above URL, which, for some reason, was to an image rather than the demo. Silly me...it now links to the actual demo. Sorry.
只是我对正则表达式的看法
Fiddle: http://jsfiddle.net/nWc2D/
Just my take with regex
Fiddle: http://jsfiddle.net/nWc2D/
试试这个:
在这种情况下
$(this)
将为您提供悬停图像,而不是该类的所有图像。try this:
In this context
$(this)
will give you the hovered image rather than all images with that class.