Step by Step
Step-by-step
I'd like to take a closer look at the step function, and for clarity, here's an example implementation that displays the current value of the animated property:
// element that will be used to display info
let $h2 = $("h2");
// our step function
let customStep = function(now, fx) {
fx.elem; // the DOM element being animated
fx.prop; // the property being animated
fx.start; // the starting value
fx.end; // the ending value
fx.pos; // the coefficient, changes from 0 to 1
fx.options; // animation settings options
now; // current value of the animated property, calculated as
// now = (fx.end - fx.start) * fx.pos
$h2.html(`${fx.prop}: ${now}${fx.unit}`); // display text
};
// call animation with a custom function
$("img").animate({ height: "-=10px" }, { step: customStep });Using this function, you can stop the animation when certain values are reached:
I've never actually had to use step functions in commercial projects — only for code examples in this textbook.
Last updated