jQuery: how do I animate a div rotation?

后端 未结 8 879
梦毁少年i
梦毁少年i 2020-11-29 05:47

I can rotate a div with css, and jquery .rotate, but i don\'t know how to animate it.

相关标签:
8条回答
  • 2020-11-29 06:38

    This works for me:

    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)');
            }
        });
    };
    
    0 讨论(0)
  • 2020-11-29 06:39

    I've been using

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

    0 讨论(0)
提交回复
热议问题