jQuery 动画
我正在使用 Jquery .animate,并尝试做一些基本的图像动画,在鼠标悬停时展开图像。然而,我有 4 张图像,目前每当一张图像展开时,它都会影响其他图像。我如何使它们彼此独立?
<div class="logos">
<img align="top" id="imgBuffalo" src="../../../../Content/Images/buffalo.png" width="80" height="60" />
<img align="top" id="imgIndianapolis" src="../../../../Content/Images/indianapolis.png" width="80" height="60" />
<img align="top" id="imgMiami" src="../../../../Content/Images/miami.png" width="80" height="60" />
<img align="top" id="imgNYJets" src="../../../../Content/Images/nyjets.png" width="80" height="60" />
</div>
$(document).ready(function () {
$("#imgBuffalo").mouseover(function () {
$("#imgBuffalo").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgBuffalo").mouseleave(function () {
$("#imgBuffalo").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgIndianapolis").mouseover(function () {
$("#imgIndianapolis").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgIndianapolis").mouseleave(function () {
$("#imgIndianapolis").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgMiami").mouseover(function () {
$("#imgMiami").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgMiami").mouseleave(function () {
$("#imgMiami").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgNYJets").mouseover(function () {
$("#imgNYJets").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgNYJets").mouseleave(function () {
$("#imgNYJets").animate({ width: "80px", height: "60px" }, 500);
});
I am playing around with Jquery .animate, and trying to do some basic image animations where I expand an image on mouse over. However, I have 4 images, and currently whenever one is expanded it effects the other images. How do I make them independent of each other?
<div class="logos">
<img align="top" id="imgBuffalo" src="../../../../Content/Images/buffalo.png" width="80" height="60" />
<img align="top" id="imgIndianapolis" src="../../../../Content/Images/indianapolis.png" width="80" height="60" />
<img align="top" id="imgMiami" src="../../../../Content/Images/miami.png" width="80" height="60" />
<img align="top" id="imgNYJets" src="../../../../Content/Images/nyjets.png" width="80" height="60" />
</div>
$(document).ready(function () {
$("#imgBuffalo").mouseover(function () {
$("#imgBuffalo").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgBuffalo").mouseleave(function () {
$("#imgBuffalo").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgIndianapolis").mouseover(function () {
$("#imgIndianapolis").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgIndianapolis").mouseleave(function () {
$("#imgIndianapolis").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgMiami").mouseover(function () {
$("#imgMiami").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgMiami").mouseleave(function () {
$("#imgMiami").animate({ width: "80px", height: "60px" }, 500);
});
$("#imgNYJets").mouseover(function () {
$("#imgNYJets").animate({ width: "90px", height: "70px" }, 500);
});
$("#imgNYJets").mouseleave(function () {
$("#imgNYJets").animate({ width: "80px", height: "60px" }, 500);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
animate
方法可以有效地更改其所作用的元素上的 CSS。由于您的图像只是彼此内嵌,因此更改其中一个图像的尺寸必然会影响其他图像。您可以尝试使用
position:relative
或position:absolute
,以便图像在尺寸变化时不会移动,但是跨浏览器会出现不一致的结果。如果您正在寻找弹出扩展类型效果,我建议创建一个新的 div,然后将图像克隆到其中。然后,您可以对浮动 div/图像执行任何您想要的操作,而不会影响其他内容。
根据您当前使用的动画,您似乎正在尝试实现类似于 Apple Dock 动画的效果。您可以使用许多可用的 jquery 插件直接手工编码动画。
Using the
animate
method effectively alters the CSS on the elements it acts on. Since your images are simply inline with each other, changing the dimensions of one, will necessarily effect the others.You could try playing with
position: relative
orposition: absolute
so that the images won't move when their dimensions change, however you're going to have inconsistent results across browsers.If you're looking for a pop-up expansion type effect, I'd suggest creating a new div and then clone the image into that. You can then do whatever you'd like to the floating div/image without having an affect on the others.
Based on the animations you're currently using, it looks like you're trying to achieve something similar to the Apple dock animations. There are a number of jquery plugins available already that you might use instead of hand coding the animations directly.
您的意思是当一个图像扩展时它会移动其他图像?如果您不希望它们互相影响,则必须将每个图像放入绝对 div 中,并将包含活动动画的 div 的 z-index 更改为位于顶部。
You mean it moves the other images as the one image is expanding? If you don't want them affecting eachother you will have to put each image into an absolute div and change the z-index of the div containing the active animation to be on top.
最好的选择是将徽标绝对放置在屏幕上。这将防止它们彼此发生冲突。给每个 img 一个“logo”类或类似的东西。
然后根据每个徽标的 ID 放置其位置。
接下来,我建议使用 .hover() 而不是 mouseover 和 mouseleave。这使您能够用更少的代码设置两个事件处理程序。
阅读 http://api.jquery.com/hover/ 了解更多详细信息。
您正走在学习 jQuery 中一些很酷的效果的正确道路上。继续努力吧!
Your best bet is to position your logos absolutely on the screen. This will keep them from conflicting with each other. Give each img a "logo" class or something like that.
Then position each logo based on its ID.
Next, I'd suggest using .hover() instead of the mouseover and mouseleave. This gives you the ability to set two event handlers in less code.
Read up on http://api.jquery.com/hover/ for more details.
You're on the right path to learning some cool effects in jQuery. Keep it up!