jquery - How to make rotate to 45 deg and animate change left same time? -
how make in duration rotate angle 45 deg , stop? can't find way make angle
in below demo increase 45 during animate change margin-left, how it??
var angle = 0; $('.btn').click(function(){ $("#a").animate ({ "margin-left": "+=200px" }, { step: function (now, fx) { angle += 1; $(this).css({ "-moz-transform":"rotate("+angle+"deg)", "-webkit-transform":"rotate("+angle+"deg)", "-ms-transform":"rotate("+angle+"deg)", "-o-transform":"rotate("+angle+"deg)" }); }, duration: 300 }, "linear"); });
there no reason increment rotation each step can use .animate
, give target value , automatically step value (and fluently)
if doing this, use .css
, css transitions performs better jquery's transform
. following
$('.btn').click(function(){ $("#a").css ({ "-moz-transform":"translatex(200px) rotate(45deg)", "-webkit-transform":"translatex(200px) rotate(45deg)", "-ms-transform":"translatex(200px) rotate(45deg)", "transform":"translatex(200px) rotate(45deg)" }); });
#a { margin-top:25px; margin-left:25px; width: 150px; height: 150px; list-style: none; background-color: blue; -webkit-transition:.3s linear; -moz-transition:.3s linear; -ms-transition:.3s linear; transition:.3s linear; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="btn">btn</div> <ul> <li id="a"> <span>content</span> </li> </ul>
for performance recommend using vanilla javascript well
Comments
Post a Comment