jQuery: how do I animate a div rotation?

后端 未结 8 978
梦毁少年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: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 }
    
    potato
    
    
    

提交回复
热议问题