jQuery: how do I animate a div rotation?

后端 未结 8 1025
梦毁少年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:30

    As of now you still can't animate rotations with jQuery, but you can with CSS3 animations, then simply add and remove the class with jQuery to make the animation occur.

    JSFiddle demo


    HTML

    
    

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

提交回复
热议问题