I'm working on creating a cross-browser compatible rotation (ie9+) and I have the following code in a jsfiddle

$(document).ready(function () { 
    DoRotate(30);
    AnimateRotate(30);
});

function DoRotate(d) {

    $("#MyDiv1").css({
          '-moz-transform':'rotate('+d+'deg)',
          '-webkit-transform':'rotate('+d+'deg)',
          '-o-transform':'rotate('+d+'deg)',
          '-ms-transform':'rotate('+d+'deg)',
          'transform': 'rotate('+d+'deg)'
     });  
}

function AnimateRotate(d) {

        $("#MyDiv2").animate({
          '-moz-transform':'rotate('+d+'deg)',
          '-webkit-transform':'rotate('+d+'deg)',
          '-o-transform':'rotate('+d+'deg)',
          '-ms-transform':'rotate('+d+'deg)',
          'transform':'rotate('+d+'deg)'
     }, 1000); 
}

The CSS and HTML are really simple and just for demo:

.SomeDiv{
    width:50px;
    height:50px;       
    margin:50px 50px;
    background-color: red;}

<div id="MyDiv1" class="SomeDiv">test</div>
<div id="MyDiv2" class="SomeDiv">test</div>

使用.css()时旋转有效,但使用.animate()时旋转无效;为什么会这样?有没有办法解决?

谢谢

推荐答案

CSS-Transforms are not possible to animate with jQuery, yet. You can do something like this:

function AnimateRotate(angle) {
    // caching the object for performance reasons
    var $elem = $('#MyDiv2');

    // we use a pseudo object for the animation
    // (starts from `0` to `angle`), you can name it as you want
    $({deg: 0}).animate({deg: angle}, {
        duration: 2000,
        step: function(now) {
            // in the step-callback (that is fired each step of the animation),
            // you can use the `now` paramter which contains the current
            // animation-position (`0` up to `angle`)
            $elem.css({
                transform: 'rotate(' + now + 'deg)'
            });
        }
    });
}

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

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

顺便说一句:您不需要在CSS3转换前面加上jQuery 1.7+

使现代化

You can wrap this in a jQuery-plugin to make your life a bit easier:

$.fn.animateRotate = function(angle, duration, easing, complete) {
  return this.each(function() {
    var $elem = $(this);

    $({deg: 0}).animate({deg: angle}, {
      duration: duration,
      easing: easing,
      step: function(now) {
        $elem.css({
           transform: 'rotate(' + now + 'deg)'
         });
      },
      complete: complete || $.noop
    });
  });
};

$('#MyDiv2').animateRotate(90);

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

使现代化2

我对它进行了一些优化,使easingdurationcomplete的顺序变得无关紧要.

$.fn.animateRotate = function(angle, duration, easing, complete) {
  var args = $.speed(duration, easing, complete);
  var step = args.step;
  return this.each(function(i, e) {
    args.complete = $.proxy(args.complete, e);
    args.step = function(now) {
      $.style(e, 'transform', 'rotate(' + now + 'deg)');
      if (step) return step.apply(e, arguments);
    };

    $({deg: 0}).animate({deg: angle}, args);
  });
};

使现代化 2.1

Thanks to matteo who noted an issue with the this-context in the complete-callback. If fixed it by binding the callback with jQuery.proxy on each node.

我已经从使现代化 2开始在代码中添加了这个版本.

使现代化 2.2

如果您想前后切换旋转,这是一个可能的修改.我只是在函数中添加了一个start参数,并替换了这一行:

$({deg: start}).animate({deg: angle}, args);

如果有人知道如何使其更适用于所有用例,无论他们是否想要设置起始学位,请进行适当的编辑.


The Usage...is quite simple!

Mainly you've two ways to reach the desired result. But at the first, let's take a look on the arguments:

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

Except of "angle" are all of them optional and fallback to the default jQuery.fn.animate-properties:

duration: 400
easing: "swing"
complete: function () {}

第一

This way is the short one, but looks a bit unclear the more arguments we pass in.

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

第二

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

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

Jquery相关问答推荐

为什么如果使用转换规模,juserui可拖动遏制不起作用

使用 Ajax 列出数据(仅显示最后的数据)

哪个 JQuery Select 器会排除与给定 Select 器匹配的父项的项目?

Jquery each - 停止循环并返回对象

自动完成时触发的任何事件?

jQuery Keypress 箭头键

在网络浏览器中,onblur 和 onfocusout 有什么区别?

JSLint 消息:未使用的变量

从 JQuery.ajax 成功数据中解析 JSON

jQuery.parseJSON 与 JSON.parse

jQuery .load() 调用不会在加载的 HTML 文件中执行 JavaScript

在 JQuery .trigger 上传递参数

jQuery UI 工具提示不支持 html 内容

使用 Typescript 时如何停止类型 JQuery 上不存在属性语法错误?

如果不是 jQuery,Javascript 中的美元符号是什么

使用 jQuery 的 ajax 方法将图像作为 blob 检索

订单对象是否由指定的 jQuery Select 器返回?

如何使用jQuery触发类更改事件?

jQuery : eq() 与 get()

如何使用 javascript 展开和折叠