Master Simple Easing: Plug‑and‑Play Animation Algorithm for UI

This article introduces a lightweight easing algorithm that moves an element by half the remaining distance each frame, provides a requestAnimationFrame polyfill, a reusable Math.easeout function, and shows real‑world usage in Qidian's scroll‑to‑top and other UI effects.

Yuewen Frontend Team
Yuewen Frontend Team
Yuewen Frontend Team
Master Simple Easing: Plug‑and‑Play Animation Algorithm for UI

After a whimsical story, the article introduces a simple easing algorithm for UI animations, where the position moves by half the remaining distance each frame.

The basic formula is: A = A + (B - A) / 2 In words: next position = current position + half of the distance to the target.

A requestAnimationFrame polyfill ensures compatibility:

if (!window.requestAnimationFrame) {
    requestAnimationFrame = function(fn) {
        setTimeout(fn, 17);
    };
}

The generalized version uses a variable rate: A = A + (B - A) / rate A reusable function Math.easeout is provided:

Math.easeout = function(A, B, rate, callback) {
    if (A == B || typeof A != 'number') return;
    B = B || 0;
    rate = rate || 2;
    var step = function() {
        A = A + (B - A) / rate;
        if (A < 1) {
            callback(B, true);
            return;
        }
        callback(A, false);
        requestAnimationFrame(step);
    };
    step();
};

Example usage for a “back‑to‑top” animation:

var doc = document.body.scrollTop ? document.body : document.documentElement;
Math.easeout(doc.scrollTop, 0, 4, function(value) {
    doc.scrollTop = value;
});

The same algorithm powers various effects on the Qidian mobile site, such as the scroll‑to‑top button, the “recently read” ball movement, and page‑turning animations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontendanimationUIJavaScriptrequestAnimationFrameEasing
Yuewen Frontend Team
Written by

Yuewen Frontend Team

Click follow to learn the latest frontend insights in the cultural content industry. We welcome you to join us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.