jQuery旋转/转换

And*_*rea 21 jquery transform rotation

我想使用此功能旋转然后在特定点或度数停止.现在元素只是旋转而不停止.这是代码:

    <script type="text/javascript">
    $(function() {
       var $elie = $("#bkgimg");
       rotate(0);
       function rotate(degree) {

           // For webkit browsers: e.g. Chrome
           $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
           // For Mozilla browser: e.g. Firefox
           $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});

           // Animate rotation with a recursive call
           setTimeout(function() { rotate(++degree); },65);
       }
    });
    </script>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Spa*_*rky 36

只需删除一次旋转一度的线并永远调用脚本.

// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
Run Code Online (Sandbox Code Playgroud)

然后将所需的值传递给函数...在此示例中45为45度.

$(function() {

    var $elie = $("#bkgimg");
    rotate(45);

        function rotate(degree) {
      // For webkit browsers: e.g. Chrome
           $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
      // For Mozilla browser: e.g. Firefox
           $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
        }

});
Run Code Online (Sandbox Code Playgroud)

改变.css().animate()动画与jQuery旋转.我们还需要为动画添加持续时间5000秒,持续5秒.并更新您的原始功能以删除一些冗余并支持更多浏览器...

$(function() {

    var $elie = $("#bkgimg");
    rotate(45);

        function rotate(degree) {
            $elie.animate({
                        '-webkit-transform': 'rotate(' + degree + 'deg)',
                        '-moz-transform': 'rotate(' + degree + 'deg)',
                        '-ms-transform': 'rotate(' + degree + 'deg)',
                        '-o-transform': 'rotate(' + degree + 'deg)',
                        'transform': 'rotate(' + degree + 'deg)',
                        'zoom': 1
            }, 5000);
        }
});
Run Code Online (Sandbox Code Playgroud)

编辑:上面 的标准jQuery CSS动画代码不起作用,因为显然,jQuery .animate()还不支持CSS3 transforms.

这个jQuery插件应该为旋转设置动画:

http://plugins.jquery.com/project/QTransform


swi*_*itz 11

这是因为你在rotate中有一个递归函数.它再次呼唤自己:

// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
Run Code Online (Sandbox Code Playgroud)

拿出它,它不会继续递归运行.

我也建议只使用这个功能:

function rotate($el, degrees) {
    $el.css({
  '-webkit-transform' : 'rotate('+degrees+'deg)',
     '-moz-transform' : 'rotate('+degrees+'deg)',  
      '-ms-transform' : 'rotate('+degrees+'deg)',  
       '-o-transform' : 'rotate('+degrees+'deg)',  
          'transform' : 'rotate('+degrees+'deg)',  
               'zoom' : 1

    });
}
Run Code Online (Sandbox Code Playgroud)

它更干净,适用于大多数浏览器.


cpi*_*cpi 7

为什么不单击使用toggleClass?

JS:

$(this).toggleClass("up");
Run Code Online (Sandbox Code Playgroud)

CSS:

button.up {
    -webkit-transform: rotate(180deg);
       -moz-transform: rotate(180deg);
        -ms-transform: rotate(180deg);
         -o-transform: rotate(180deg);
            transform: rotate(180deg);
               /* IE6–IE9 */
               filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.9914448613738104, M12=-0.13052619222005157,M21=0.13052619222005157, M22=0.9914448613738104, sizingMethod='auto expand');
                 zoom: 1;
    }
Run Code Online (Sandbox Code Playgroud)

你也可以将它添加到css:

button{
        -webkit-transition: all 500ms ease-in-out;
        -moz-transition: all 500ms ease-in-out;
        -o-transition: all 500ms ease-in-out;
        -ms-transition: all 500ms ease-in-out;
}
Run Code Online (Sandbox Code Playgroud)

这将添加动画.

PS ...

回答你原来的问题:

你说它旋转但从未停止过.使用set timeout时,您需要确保您的条件不会调用settimeout,否则它将永远运行.所以对于你的代码:

<script type="text/javascript">
    $(function() {
     var $elie = $("#bkgimg");
     rotate(0);
     function rotate(degree) {

      // For webkit browsers: e.g. Chrome
    $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
      // For Mozilla browser: e.g. Firefox
    $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});


    /* add a condition here for the extremity */ 
    if(degree < 180){
      // Animate rotation with a recursive call
      setTimeout(function() { rotate(++degree); },65);
     }
    }
    });
    </script>
Run Code Online (Sandbox Code Playgroud)