• <legend id='LkldJ'><style id='LkldJ'><dir id='LkldJ'><q id='LkldJ'></q></dir></style></legend>

      • <bdo id='LkldJ'></bdo><ul id='LkldJ'></ul>

      <tfoot id='LkldJ'></tfoot>
      1. <small id='LkldJ'></small><noframes id='LkldJ'>

        <i id='LkldJ'><tr id='LkldJ'><dt id='LkldJ'><q id='LkldJ'><span id='LkldJ'><b id='LkldJ'><form id='LkldJ'><ins id='LkldJ'></ins><ul id='LkldJ'></ul><sub id='LkldJ'></sub></form><legend id='LkldJ'></legend><bdo id='LkldJ'><pre id='LkldJ'><center id='LkldJ'></center></pre></bdo></b><th id='LkldJ'></th></span></q></dt></tr></i><div id='LkldJ'><tfoot id='LkldJ'></tfoot><dl id='LkldJ'><fieldset id='LkldJ'></fieldset></dl></div>

        带有 jquery.animate() 的 CSS 旋转跨浏览器

        时间:2023-10-13

          <bdo id='aRdsZ'></bdo><ul id='aRdsZ'></ul>
          <i id='aRdsZ'><tr id='aRdsZ'><dt id='aRdsZ'><q id='aRdsZ'><span id='aRdsZ'><b id='aRdsZ'><form id='aRdsZ'><ins id='aRdsZ'></ins><ul id='aRdsZ'></ul><sub id='aRdsZ'></sub></form><legend id='aRdsZ'></legend><bdo id='aRdsZ'><pre id='aRdsZ'><center id='aRdsZ'></center></pre></bdo></b><th id='aRdsZ'></th></span></q></dt></tr></i><div id='aRdsZ'><tfoot id='aRdsZ'></tfoot><dl id='aRdsZ'><fieldset id='aRdsZ'></fieldset></dl></div>
        • <small id='aRdsZ'></small><noframes id='aRdsZ'>

            <tfoot id='aRdsZ'></tfoot>

                  <legend id='aRdsZ'><style id='aRdsZ'><dir id='aRdsZ'><q id='aRdsZ'></q></dir></style></legend>
                    <tbody id='aRdsZ'></tbody>
                1. 本文介绍了带有 jquery.animate() 的 CSS 旋转跨浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在创建一个跨浏览器兼容的旋转 (ie9+),我在 jsfiddle 中有以下代码

                  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); 
                  }
                  

                  CSS 和 HTML 非常简单,仅用于演示:

                  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() 时无效;为什么会这样?有办法解决吗?

                  The rotation works when using .css() but not when using .animate(); why is that and is there a way to fix it?

                  谢谢.

                  推荐答案

                  目前尚无法使用 jQuery 制作 CSS-Transforms 动画.你可以这样做:

                  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)'
                              });
                          }
                      });
                  }
                  

                  您可以在此处阅读有关 step-callback 的更多信息:http://api.jquery.com/动画/#step

                  You can read more about the step-callback here: http://api.jquery.com/animate/#step

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

                  而且,顺便说一句:您不需要为 jQuery 1.7+ 的 css3 转换添加前缀

                  And, btw: you don't need to prefix css3 transforms with jQuery 1.7+

                  您可以将其包装在一个 jQuery 插件中,让您的生活更轻松:

                  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

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

                  I optimized it a bit to make the order of easing, duration and complete insignificant.

                  $.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

                  感谢 matteo谁注意到完整-callback 中的this-context 存在问题.如果通过 binding 在每个节点上使用 jQuery.proxy 的回调来修复它.

                  Update 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.

                  我在 Update 2 之前已将版本添加到代码中.

                  I've added the edition to the code before from Update 2.

                  如果您想要执行诸如来回切换旋转之类的操作,这是一个可能的修改.我只是在函数中添加了一个 start 参数并替换了这一行:

                  This is a possible modification if you want to do something like toggle the rotation back and forth. I simply added a start parameter to the function and replaced this line:

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

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

                  If anyone knows how to make this more generic for all use cases, whether or not they want to set a start degree, please make the appropriate edit.

                  主要有两种方法可以达到预期的结果.但首先,让我们看一下论据:

                  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(角度、持续时间、缓动、完成)

                  除了角度"之外,它们都是可选的并且回退到默认的jQuery.fn.animate-properties:

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

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

                  第一个

                  这种方式比较短,但是传入的参数越多看起来就有点不清楚了.

                  1st

                  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 () {});
                  

                  第二次

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

                  2nd

                  I prefer to use objects if there are more than three arguments, so this syntax is my favorit:

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

                  这篇关于带有 jquery.animate() 的 CSS 旋转跨浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在jQuery中获取元素-moz-transform:旋转值 下一篇:CSS3 变换:旋转;在 IE9 中

                  相关文章

                  最新文章

                2. <legend id='Nr9gG'><style id='Nr9gG'><dir id='Nr9gG'><q id='Nr9gG'></q></dir></style></legend>
                  • <bdo id='Nr9gG'></bdo><ul id='Nr9gG'></ul>

                    <i id='Nr9gG'><tr id='Nr9gG'><dt id='Nr9gG'><q id='Nr9gG'><span id='Nr9gG'><b id='Nr9gG'><form id='Nr9gG'><ins id='Nr9gG'></ins><ul id='Nr9gG'></ul><sub id='Nr9gG'></sub></form><legend id='Nr9gG'></legend><bdo id='Nr9gG'><pre id='Nr9gG'><center id='Nr9gG'></center></pre></bdo></b><th id='Nr9gG'></th></span></q></dt></tr></i><div id='Nr9gG'><tfoot id='Nr9gG'></tfoot><dl id='Nr9gG'><fieldset id='Nr9gG'></fieldset></dl></div>

                    <small id='Nr9gG'></small><noframes id='Nr9gG'>

                    <tfoot id='Nr9gG'></tfoot>