Animate translate3d with jQuery

35,825

Solution 1

Basically with animate and transform you have to use the step function of the animate function of jQuery which will look a bit like this:

$('.animate').animate({
    'opacity': '320'
}, {
    step: function (now, fx) {
        $(this).css({"transform": "translate3d(0px, " + now + "px, 0px)"});
    },
    duration: 10000,
    easing: 'linear',
    queue: false,
    complete: function () {
        alert('Animation is done');
    }
}, 'linear');

You would have to set the text-indent separately but basically what you were doing wrong was that every time the step function was called the value was set straight to 320px instead of as it goes. This can be tackled by having two separate functions and using unimportant css rules to create the values needed across the animation curve. You also need to set the queue to false so that the two animations happen at the same time and not one after the other

The final snippet of code would look like this:

$('.animate').animate({
    'opacity': '320'
}, {
    step: function (now, fx) {
        $(this).css({"transform": "translate3d(0px, " + now + "px, 0px)"});
    },
    duration: 10000,
    easing: 'linear',
    queue: false,
    complete: function () {
        alert('Animation is done');
    }
}, 'linear');
$(".animate").animate({ textIndent: 100 }, {
    duration : 10000,
    easing: 'linear',
    queue: false
});

Here is a working fiddle:

http://jsfiddle.net/Hive7/1qu2qxqh/

Solution 2

You can use jquery.transit jQuery plugin for css3 transition:

$('.box').transition({ rotate: '45deg' });
$('.box').transition({ scale: 2.2 });
$('.box').transition({ skewY: '30deg' });
$('.box').transition({
  perspective: '100px',
  rotate3d: '1,1,0,180deg'
});

very good plugin and many features

Delay, Optional units, Vendor prefixes, Chaining & queuing, Alternate easing/duration syntax, Relative values, Transformation origins and Easing

Demo Here

Share:
35,825
Rob Boerman
Author by

Rob Boerman

Updated on December 05, 2020

Comments

  • Rob Boerman
    Rob Boerman over 3 years

    I'm trying to animate translate3d with Jquery 2.1.1. in 10 seconds. Then, when the animation is complete, I want it to alert me. But the problem is that it does not animate. It just changes instantly to the new css.

    Is there some hero that can help me with this?

    The code i use now:

    $( ".circle" ).animate({ textIndent: 100 }, {
        duration : 10000,
        step : function() {
          $(this).css("transform","translate3d(0px, 320px, 0px)"); 
        }, 
        complete : function() {
            alert("completed the animation");
        }
    },'linear');
    
  • Rob Boerman
    Rob Boerman over 9 years
    you're awesome thank you so much for helping me out, i didn't get it, but now i see how the work around is build, keep being a hero :)
  • Rob Boerman
    Rob Boerman over 9 years
    One more question, is it also possible to animate from the current translate3d to the new one?
  • Hive7
    Hive7 over 9 years
    yes to transform from your current to your next all you have to do is find your current and set the value of your step to the value then animate if that makes sense @RobBoerman