简体   繁体   English

连续轮换

[英]Consecutive rotations

I have the following code that I apply on click: 我有以下适用于点击的代码:

$("#search").click(function()
{
  if (searchForth)
  {
    animateRotate(45,"#search");
    searchForth = false;
  }
  else
  {
    animateRotate(-45,"#search");
    searchForth = true;
  }
});

And the function being called: 并且该函数被调用:

function animateRotate(d,element)
{
  var elem = $(element);
  $({deg: 0}).animate({deg: d},{
      duration: 600,
      step: function(now)
      {
        elem.css({
            transform: "rotate(" + now + "deg)"
        });
      }
  });
}

When the function gets called, the element rotates 45 degrees from the default position, which is what I want. 当函数被调用时,元素从默认位置旋转45度,这就是我想要的。 However, when I click on the element again, the function applies a rotation of -45 degrees, but it does so from the default position of the element instead of the position the element was left in from the previous rotation. 但是,当我再次单击该元素时,该函数应用了-45度的旋转,但它是从元素的默认位置开始的,而不是从上一次旋转后元素保留在其中的位置。 Why is that and how can I fix it so the second animation 'picks off' from the final position of the first animation? 为什么会这样,如何解决这个问题,使第二个动画从第一个动画的最终位置“挑出来”?

  1. You should save rotation angle 您应该保存旋转角度
  2. You shouldn't find elem by deg 你不应该按度数找到元素

 var searchForth = false; var angle=0; $("#search").click(function() { if (searchForth) { angle += 45; animateRotate(angle,"#search"); searchForth = false; } else { angle -= 45; animateRotate(angle,"#search"); searchForth = true; } }); function animateRotate(d,element) { var elem = $(element); $(elem).animate({deg: d},{ duration: 600, step: function(now) { elem.css({ transform: "rotate(" + now + "deg)" }); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="search">Search</button> 

You need to get the current rotated position first and than add/remove 45 degrees from it, according to the value of searchForth . 您需要首先获取当前旋转位置,然后根据searchForth的值从该位置添加/删除45度。

Look at this for more info: Get element -moz-transform:rotate value in jQuery 查看更多信息: 在jQuery中获取元素-moz-transform:rotate值

 var searchForth=false; $("#rotate").click(function() { if (searchForth) { animateRotate(45,this); searchForth = false; } else { animateRotate(-45,this); searchForth = true; } }); function animateRotate(d,element) { var startDeg = getRotationDegrees($(element)); var elem = $(element); $(elem).animate({deg: (startDeg + d)},{ duration: 600, step: function(now) { elem.css({ transform: "rotate(" + now + "deg)" }); } }); } function getRotationDegrees(obj) { var matrix = obj.css("-webkit-transform") || obj.css("-moz-transform") || obj.css("-ms-transform") || obj.css("-o-transform") || obj.css("transform"); if(matrix !== 'none') { var values = matrix.split('(')[1].split(')')[0].split(','); var a = values[0]; var b = values[1]; var angle = Math.round(Math.atan2(b, a) * (180/Math.PI)); } else { var angle = 0; } return (angle < 0) ? angle + 360 : angle; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="rotate">Rotate me!</div> 

How about just using CSS3 transition and transform? 仅使用CSS3过渡和转换怎么样?

 $(function() { var deg = 45; var srh = $('#search').on('click', function(e) { srh.css('transform', 'rotate(' + (deg) + 'deg)'); deg = -(deg); }); }); 
 #search { transition: transform 1s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="search">Search</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM