jquery 在悬停时更改图像(然后返回初始图像)
我有一个照片库,我真的想用另一张图像更改悬停时的每张图像。 我现在从数据库动态获取图像(它实际上是图像向量)
,我使用 jquery 插件,但它不起作用。它的唯一工作方式是,如果我静态地放置 id(始终相同),但它只会更改具有单个特定图像的任何图像。我能做些什么? 我有代码:
<? foreach ($paginated_products as $product):?>
<? $image = $product->images->limit(2)->find_all(); ?>
<script>
$('img#nav<?php echo $image[1]; ?>').hover(function() {
$(this).attr("src","<?php echo $image[0]->url; ?>");
}, function() {
$(this).attr("src","<?php echo $image[1]->url; ?>");
});</script>
//this way doesn;t work at all. if i put simply img#nav , it only changes me with a single image.
<img src="<?= $image[1]->url; ?>" id ="nav<?php echo $image[1]; ?>"></img>
i have a photo gallery, and i really want to change each image on hover, with another image.
i fetch the images dynamically, from a database, (it is practically a vector of images)
right now, i use a jquery plugin, but it doesn't work. the only way it works, is if i put the id statically (always the same), but then it only changes me any image with a single specific image . what can i do?
i have the code:
<? foreach ($paginated_products as $product):?>
<? $image = $product->images->limit(2)->find_all(); ?>
<script>
$('img#nav<?php echo $image[1]; ?>').hover(function() {
$(this).attr("src","<?php echo $image[0]->url; ?>");
}, function() {
$(this).attr("src","<?php echo $image[1]->url; ?>");
});</script>
//this way doesn;t work at all. if i put simply img#nav , it only changes me with a single image.
<img src="<?= $image[1]->url; ?>" id ="nav<?php echo $image[1]; ?>"></img>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
难道你不应该把你的脚本分成两部分吗?
你的 $(...).hover() 函数无法工作,因为 jquery 尚未初始化。
PS:你能显示生成的html的一部分吗?
第一个应该是:
然后你的身体就会有第二部分(图片应该放在哪里)
Shouldn't you separe your script in 2 parts.
You're $(...).hover() functions could not work because jquery is not yet initialized.
PS: could you display a part of the generated html.
The first one should like:
Then you'll have second part in your body (where pictures should go)
您最好为替代方案提供键/值对象。
You would be better off providing a key/value object for alternatives.
某处似乎不匹配,根据您的评论
$image[1]
是一个整数,但根据您的代码,它是一个对象。如果是对象,
nav
会给你留下很多相同的 id,这解释了你遇到的问题。最有可能的是(我没有看到你的类...)id 存储在对象中的某个位置,因此你必须使用类似
$image[1]->id
而不是 <代码>$image[1]。There seems to be a mismatch somewhere, according to your comment
$image[1]
is an integer but according to your code, it is an object.If it is an object,
nav<?php echo $image[1]; ?>
will leave you with a lot of identical id's which explains the problem you're having.Most likely (I haven´t seen your classes...) the id is stored somewhere in the object, so you would have to use something like
$image[1]->id
instead of$image[1]
.如果每个图像都有一个链接,则可以为每个图像指定一个类,并在鼠标悬停时迭代它们。以下是如何使用 3 个图像执行此操作的示例:
http://jsfiddle.net/briguy37/QSQ62/
更新
另外,这里有一个示例,您可以直接使用 JQuery 更改背景图像,这样您就不必编写一堆 CSS 类定义。您必须修改它以调用适当的图像 URL 以从数据库加载它们:
http:// jsfiddle.net/briguy37/QSQ62/3/
If you had a link for each image, you could specify a class for each image and iterate through them when you hover. Here's an example of how to do this with 3 images:
http://jsfiddle.net/briguy37/QSQ62/
UPDATE
Also, here's an example where you can change the background image directly with JQuery so you don't have to write a bunch of CSS class definitions. You'll have to modify this to call the appropriate image URLs to load them from the database:
http://jsfiddle.net/briguy37/QSQ62/3/
如果您使用精灵而不是单独的图像,图像更改会更快,因为浏览器将所有精灵作为一张图像同时加载,然后您只需使用 jquery 更改背景位置即可。
The image changes will be faster if you use sprites instead of separate images, since the browser loads all of them at the same time as one image, then you can just change the background-position using jquery.
这是一种方法(除非您不想通过 jQuery 添加
元素)
That is one way of doing it (unless you don't want to add in the
<img>
element in through jQuery)