围绕一个点旋转 div
我想要动画围绕特定点旋转 div。我一直在尝试 jQuery 中的 animate() 方法来让它工作。
我尝试过使用 {rotate: "+=9deg"}
制作动画,但它不起作用。还尝试使用 MozTransform...
但它不起作用。
但是,这样的方法确实有效: {'-moz-transform': 'rotate('+ Degrees+'deg)'}
我用更简单、更直观的方法做错了什么吗?还是jquery的一个bug?或者这是一种荒谬的方式,我应该采取“硬方法”?
I want to animate rotating a div about a particular point. I've been trying the animate()
method in jQuery to get this to work.
I've tried animating with {rotate: "+=9deg"}
but it doesn't work. Also tried using MozTransform...
and it didn't work.
However, something like this does work: {'-moz-transform': 'rotate('+degrees+'deg)'}
Is there something I'm doing wrong with the simpler, more intuitive ways? Or is it a bug in jquery? Or is it an absurd way and I should do it the "hardway"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是让所有当前浏览器旋转元素所需的完整 CSS 样式表。我已经对它们影响的相关浏览器的样式进行了评论。
此示例将元素旋转 45 度。 IE6/7/8
filter
和-ms-filter
样式使用弧度而不是度数。filter
样式与其他样式之间的另一个大区别是旋转的原点。如果我没记错的话,transform
样式围绕中心旋转,而 MS 样式围绕左上角旋转。我相信这是可以调整的,但我们只是通过使用仅 IE 的样式来解决这个问题,该样式改变了元素的绝对位置,使其最终位于相同的位置。显然,这对于动画来说不会有帮助。最后要指出的是,IE6/7
filter
样式实际上是无效的 CSS(因为它包含冒号和其他保留字符)。 IE8 的-ms-filter
变体是可以的,因为它在引号中,但在 IE6/7 版本中不允许这样做。这个问题实际上破坏了 Firefox(也可能是其他浏览器)中的 CSS 解析,以至于无法读取任何后续样式。出于这个原因,我建议您将它(或至少是 IE 特定的部分)包含在它自己的样式表中。Here is the full CSS stylesheet you need to get all current browsers to rotate an element. I've commented the styles with the relevant browsers that they affect.
This example rotates an element by 45 degrees. The IE6/7/8
filter
and-ms-filter
styles use radians instead of degrees.The other big difference between the
filter
styles and the others is the origin point of the rotation. If I recall correctly, thetransform
styles rotate around the center while the MS styles rotate around the top left corner. I believe this can be adjusted, but we solved this issue just by having an IE-only style that shifted the absolute position of the element so it ended up in the same place. Obviously this won't help you when it comes to an animation though.The final thing to point out is that the IE6/7
filter
style is actually invalid CSS (because it contains colons and other reserved characters). The-ms-filter
variant for IE8 is okay because it is in quotes, but this isn't allowed in the IE6/7 version. This problem actually breaks CSS parsing in Firefox (and possibly other browsers), to the point that any subsequent styles will not be read. For this reason, I recommend that you include it (or at least the IE-specific parts) in its own stylesheet.找到了一个完全可以做到这一点的插件:
https://github.com/heygrady/transform/wiki/
它允许您使用标准 animate() 函数来制作动画变换!
Found a plugin that does exactly this:
https://github.com/heygrady/transform/wiki/
It lets you animate transforms using the standard animate() function!
要绕点旋转,请使用transform-origin 属性。例如,以下将设置围绕左下点的旋转。
To rotate about a point, use the transform-origin property. For instance, the following will set rotation around bottom-left point.
rotate
不是 CSS 属性,因此.animate()
无法使用它。 (.animate()
只是逐渐将指定的 CSS 属性从一个值更改为另一个值。)rotate
isn't a CSS property, so.animate()
will not work with it. (.animate()
just gradually changes specified CSS properties from one value to another.)