CSS旋转浏览器与jquery.animate()

前端之家收集整理的这篇文章主要介绍了CSS旋转浏览器与jquery.animate()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个跨浏览器兼容的旋转(ie9),我有一个 jsfiddle以下代码
  1. $(document).ready(function () {
  2. DoRotate(30);
  3. AnimateRotate(30);
  4. });
  5.  
  6. function DoRotate(d) {
  7.  
  8. $("#MyDiv1").css({
  9. '-moz-transform':'rotate('+d+'deg)','-webkit-transform':'rotate('+d+'deg)','-o-transform':'rotate('+d+'deg)','-ms-transform':'rotate('+d+'deg)','transform': 'rotate('+d+'deg)'
  10. });
  11. }
  12.  
  13. function AnimateRotate(d) {
  14.  
  15. $("#MyDiv2").animate({
  16. '-moz-transform':'rotate('+d+'deg)','transform':'rotate('+d+'deg)'
  17. },1000);
  18. }

CSS和HTML真的很简单,只是为了演示:

  1. .SomeDiv{
  2. width:50px;
  3. height:50px;
  4. margin:50px 50px;
  5. background-color: red;}
  6.  
  7. <div id="MyDiv1" class="SomeDiv">test</div>
  8. <div id="MyDiv2" class="SomeDiv">test</div>

使用.css()时旋转工作,但使用.animate()时不工作。为什么是这样,有办法解决它吗?

谢谢。

解决方法

CSS – 转换是不可能的动画与jQuery,但。你可以这样做:
  1. function AnimateRotate(angle) {
  2. // caching the object for performance reasons
  3. var $elem = $('#MyDiv2');
  4.  
  5. // we use a pseudo object for the animation
  6. // (starts from `0` to `angle`),you can name it as you want
  7. $({deg: 0}).animate({deg: angle},{
  8. duration: 2000,step: function(now) {
  9. // in the step-callback (that is fired each step of the animation),// you can use the `now` paramter which contains the current
  10. // animation-position (`0` up to `angle`)
  11. $elem.css({
  12. transform: 'rotate(' + now + 'deg)'
  13. });
  14. }
  15. });
  16. }

您可以在这里阅读更多关于步骤回调的信息:http://api.jquery.com/animate/#step

http://jsfiddle.net/UB2XR/23/

和,btw:你不需要使用jQuery 1.7前缀css3 transforms

更新

你可以在jQuery插件包装,使你的生活有点简单:

  1. $.fn.animateRotate = function(angle,duration,easing,complete) {
  2. return this.each(function() {
  3. var $elem = $(this);
  4.  
  5. $({deg: 0}).animate({deg: angle},{
  6. duration: duration,easing: easing,step: function(now) {
  7. $elem.css({
  8. transform: 'rotate(' + now + 'deg)'
  9. });
  10. },complete: complete || $.noop
  11. });
  12. });
  13. };
  14.  
  15. $('#MyDiv2').animateRotate(90);

http://jsbin.com/ofagog/2/edit

Update2

我优化了一点,使顺序的顺序,持续时间和完全不重要。

  1. $.fn.animateRotate = function(angle,complete) {
  2. var args = $.speed(duration,complete);
  3. var step = args.step;
  4. return this.each(function(i,e) {
  5. args.complete = $.proxy(args.complete,e);
  6. args.step = function(now) {
  7. $.style(e,'transform','rotate(' + now + 'deg)');
  8. if (step) return step.apply(e,arguments);
  9. };
  10.  
  11. $({deg: 0}).animate({deg: angle},args);
  12. });
  13. };

更新2.1

感谢matteo谁注意到这个上下文在完整的回调中的问题。如果固定它通过绑定回调与jQuery.proxy在每个节点上。

我从更新2之前将版本添加代码

用法…很简单!

主要有两种方法来达到所需的结果。但首先,让我们来看看参数:

jQuery.fn.animateRotate(angle,duration,easing,complete)

除了“角度”都是可选的,并回退到默认的jQuery.fn.animate-properties:

  1. duration: 400
  2. easing: "swing"
  3. complete: function () {}

第一

这种方式是短的,但看起来有点不清楚我们传递的参数越多。

  1. $(node).animateRotate(90);
  2. $(node).animateRotate(90,function () {});
  3. $(node).animateRotate(90,1337,'linear',function () {});

第二

我喜欢使用对象,如果有三个以上的参数,所以这种语法是我的最爱:

  1. $(node).animateRotate(90,{
  2. duration: 1337,easing: 'linear',complete: function () {},step: function () {}
  3. });

猜你在找的CSS相关文章