0

When I try to animate translateX directly, it shows the wrong Y position and no animation. But it works fine I use $.css('transfom', 'translate(x,y)') inside begin and complete options. Why doesn't the first method work?

does not work:

$("#element").velocity({
  translateX: '15px'
}, {
  duration: 150,
  easing: "swing"
}).velocity("reverse");
#element {
  margin: 0;
  top: 50%;
  left: 15px;
  transform: translate(0, -50%);
  position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.5.2/velocity.min.js"></script>

<div id="element">item</div>

this works:

$("#element").velocity({
  left: "+=0px"
}, {
  duration: 150,
  easing: "swing",
  begin: function() {
    $(this).css('transform', 'translate(15px,-50%)')
  },
  complete: function() {
    $(this).css('transform', 'translate(0,-50%)')
  }
});
#element {
  margin: 0;
  top: 50%;
  left: 15px;
  transform: translate(0, -50%);
  position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.5.2/velocity.min.js"></script>

<div id="element">item</div>
  • finally, i am using `$("#element").velocity({left:"+=0px"},{duration:150,easing:"swing",begin:function(){$(this).css('transform','translate(15px,-50%)')},complete:function(){$(this).css('transform','translate(0,-50%)')}});` to do the work even if what i want is to animate the transform property directly... But, i will listen for any proposition ! –  Aug 22 '20 at 21:05
  • I get an error in your first example: "Attempting to reverse an animation on an element with no previous animation". Which version of Velocity are you using? These might be relevant: [Translate not working in Velocity.js](https://stackoverflow.com/questions/49837580/translate-not-working-in-velocity-js) and [How to animate translate properties with Velocity.js](https://stackoverflow.com/questions/48964932/how-to-animate-translate-properties-with-velocity-js). – showdev Aug 22 '20 at 22:27
  • i am using velocity.js version 1.5.2 –  Aug 23 '20 at 00:48

0 Answers0