jQuery:动画边距自动? [英] jQuery: Animate Margins to Auto?

查看:133
本文介绍了jQuery:动画边距自动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为图片设置动画,使其处于中心位置。这里是我想使用的代码:

I'm trying to animate an image so that it centers itself. Here's the code I'd like to use:

$('#myImage').animate({'margin-right': 'auto'});

但是当我这样做时,它被忽略,并且不会更改边距。

是否有办法将边距设为自动,或将图片对中?

But when I do that, it's ignored and doesn't change the margin.
Is there a way to animate a margin to auto, or otherwise center an image?

谢谢! / p>

Thanks!

推荐答案

由于'auto'不是数字,jQuery不能对其进行动画处理。

As 'auto' isn't a number, jQuery cannot animate it.

如果您可以将图片从文档流中移除,则可以将位置设置为绝对或固定,然后尝试:

If you are okay with taking the image out of the flow of the document, you can set position to absolute or fixed and try:

$('#myImage').animate({'left': '50%', 'margin-left': -$('#myImage').width()/2 });

这篇关于jQuery:动画边距自动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆