代码之家  ›  专栏  ›  技术社区  ›  NullVoxPopuli

jquery:如何设置DIV旋转动画?

  •  28
  • NullVoxPopuli  · 技术社区  · 15 年前

    我可以用css和jquery来旋转一个div。rotate,但我不知道如何对它进行动画处理。

    8 回复  |  直到 11 年前
        1
  •  24
  •   Steve Robbins    11 年前

    利用 WebkitTransform / -moz-transform: rotate(Xdeg) . 这在IE中不起作用,但Matt的 zachstronaut 解决方案在IE中也不起作用。

    如果你也想支持IE,你就得考虑使用 canvas 就像我相信的 Raphael does .

    下面是一个简单的jquery片段,它旋转jquery对象中的元素。旋转可以启动和停止:

    $(function() {
        var $elie = $("img"), degree = 0, timer;
        rotate();
        function rotate() {
            
            $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});  
            $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});                      
            timer = setTimeout(function() {
                ++degree; rotate();
            },5);
        }
        
        $("input").toggle(function() {
            clearTimeout(timer);
        }, function() {
            rotate();
        });
    }); 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <input type="button" value=" Toggle Spin " />
    <br/><br/><br/><br/>
    <img src="http://i.imgur.com/ABktns.jpg" />
        2
  •  14
  •   inorganik    14 年前

    如果您是为iOS设备或WebKit设计的,那么您可以在不使用JS的情况下进行设计:

    CSS:

    @-webkit-keyframes spin {  
    from {  
        -webkit-transform: rotate(0deg);  
    }  
    to {  
        -webkit-transform: rotate(360deg);  
        } 
    }
    
    .wheel {
        width:40px;
        height:40px;
        background:url(wheel.png);
        -webkit-animation-name: spin; 
        -webkit-animation-iteration-count: infinite; 
        -webkit-animation-timing-function: linear;
        -webkit-animation-duration: 3s; 
    }
    

    这将在加载时触发动画。如果你想在悬停时触发它,它可能看起来像这样:

    .wheel {
        width:40px;
        height:40px;
        background:url(wheel.png);
    }
    
    .wheel:hover {
        -webkit-animation-name: spin; 
        -webkit-animation-iteration-count: infinite; 
        -webkit-animation-timing-function: ease-in-out;
        -webkit-animation-duration: 3s; 
    }
    
        3
  •  9
  •   Steve Robbins    11 年前

    我一直在使用

    $.fn.rotate = function(degrees, step, current) {
        var self = $(this);
        current = current || 0;
        step = step || 5;
        current += step;
        self.css({
            '-webkit-transform' : 'rotate(' + current + 'deg)',
            '-moz-transform' : 'rotate(' + current + 'deg)',
            '-ms-transform' : 'rotate(' + current + 'deg)',
            'transform' : 'rotate(' + current + 'deg)'
        });
        if (current != degrees) {
            setTimeout(function() {
                self.rotate(degrees, step, current);
            }, 5);
        }
    };
    
    $(".r90").click(function() { $("span").rotate(90) });
    $(".r0").click(function() { $("span").rotate(0, -5, 90) });
    span { display: inline-block }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <span>potato</span>
    
    <button class="r90">90 degrees</button>
    <button class="r0">0 degrees</button>
        4
  •  6
  •   forresto    13 年前

    如果您只需要一个jquery选项,这将起作用:

    $(el).stop().animate(
      {rotation: 360},
      {
        duration: 500,
        step: function(now, fx) {
          $(this).css({"transform": "rotate("+now+"deg)"});
        }
      }
    );
    

    这与jquery 1.8一起工作,它自动处理CSS前缀。jquery没有动画旋转,所以我把 transform:rotate() 在习俗中 step 功能。它可能只能从0开始工作。

    演示: http://jsfiddle.net/forresto/ShUgD/

    IE9和MobileSafari4支持CSS转换,但不支持CSS转换,因此我使用Modernizer功能测试开发了这个简单的填充程序:

    if (Modernizr.csstransitions) {
      $(el).css({
        "transition": "all 500ms ease-in-out"
      });
    }
    
    $(el).click(function(){
      var rotateTo = 360;
      if (Modernizr.csstransitions) {
        $(el).css({"transform": "rotate("+rotateTo+"deg)"});
      } else {
        $(el).stop().animate(
          {rotation: rotateTo},
          {
            duration: 500,
            step: function(now, fx) {
              $(this).css({"transform": "rotate("+now+"deg)"});
            }
          }
        );
      }
    });
    

    以上将在可用时使用CSS转换。

        5
  •  3
  •   luc    13 年前

    基于PeterAjtai的答案,这里有一个小的jquery插件,可以帮助其他人。我没有在Opera和IE9上进行测试,但它也可以在这些浏览器上运行。

    $.fn.rotate = function(until, step, initial, elt) {
        var _until = (!until)?360:until;
        var _step = (!step)?1:step;
        var _initial = (!initial)?0:initial;
        var _elt = (!elt)?$(this):elt;
    
        var deg = _initial + _step;
    
        var browser_prefixes = ['-webkit', '-moz', '-o', '-ms'];
        for (var i=0, l=browser_prefixes.length; i<l; i++) {
          var pfx = browser_prefixes[i]; 
          _elt.css(pfx+'-transform', 'rotate('+deg+'deg)');
        }
    
        if (deg < _until) {
          setTimeout(function() {
              $(this).rotate(_until, _step, deg, _elt); //recursive call
          }, 5);
        }
    };
    
    $('.my-elt').rotate()
    
        6
  •  2
  •   Josh Crozier HBP    12 年前

    这对我很有用:

    function animateRotate (object,fromDeg,toDeg,duration){
        var dummy = $('<span style="margin-left:'+fromDeg+'px;">')
        $(dummy).animate({
            "margin-left":toDeg+"px"
        },{
            duration:duration,
            step: function(now,fx){
                $(object).css('transform','rotate(' + now + 'deg)');
            }
        });
    };
    
        7
  •  2
  •   Daan    11 年前

    到现在为止你还是 不能 动画旋转 使用jQuery 但你 CSS3的CAN 动画,然后简单地 使用jquery添加和移除类以使动画发生 .

    JSFiddle demo


    HTML

    <img src="http://puu.sh/csDxF/2246d616d8.png" width="30" height="30"/>
    

    CSS3

    img {
    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    -o-transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    transform: rotate(-90deg);
    transition-duration:0.4s;
    }
    
    .rotate {
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    -ms-transform: rotate(0deg);
    transform: rotate(0deg);
    transition-duration:0.4s;
    }
    

    JQuery

    $(document).ready(function() {
        $("img").mouseenter(function() {
            $(this).addClass("rotate");
        });
        $("img").mouseleave(function() {
            $(this).removeClass("rotate");
        });
    });
    
        8
  •  0
  •   Joseph Astrahan    11 年前

    我需要旋转一个对象,但有一个回调函数。受约翰·克恩回答的启发,我创造了这个。

    function animateRotate (object,fromDeg,toDeg,duration,callback){
            var dummy = $('<span style="margin-left:'+fromDeg+'px;">')
            $(dummy).animate({
                "margin-left":toDeg+"px"
            },
            {
                duration:duration,
                step: function(now,fx){
                    $(object).css('transform','rotate(' + now + 'deg)');
                    if(now == toDeg){
                        if(typeof callback == "function"){
                            callback();
                        }
                    }
                }
            }
        )};
    

    这样做,你可以简单地调用对象上的旋转,就像这样…(在我的例子中,我是在一个已经默认旋转到270度的公开三角形图标上进行的,我是在1000毫秒的时间内将它再旋转90度到360度。最后一个参数是动画完成后的回调。

    animateRotate($(".disclosure_icon"),270,360,1000,function(){
                    alert('finished rotate');
                });