Home > Web Front-end > JS Tutorial > body text

Learn animations and transitions in JavaScript

王林
Release: 2023-11-03 08:01:49
Original
1114 people have browsed it

Learn animations and transitions in JavaScript

Learning animation effects and transition effects in JavaScript requires specific code examples

In web design and development, animation effects and transition effects are one of the very important elements. one. They can make web pages more lively and interesting, attract users' attention, and improve user experience. JavaScript is a powerful programming language that can help us achieve a variety of stunning animations and transition effects. This article will introduce some common animation effects and transition effects, and provide corresponding code examples for everyone to learn and refer to.

1. Fade effect
The fade effect is a common animation effect that is used to provide a smooth transition effect when an element is shown or hidden. The following is an example of JavaScript code that implements the fade-in and fade-out effect:

// 淡入效果
function fadeIn(element, duration) {
    element.style.opacity = 0;
    element.style.display = 'block';

    var start = performance.now();

    requestAnimationFrame(function fade() {
        var now = performance.now();
        var elapsed = now - start;
        var progress = elapsed / duration;

        element.style.opacity = Math.min(progress, 1);

        if (progress < 1) {
            requestAnimationFrame(fade);
        }
    });
}

// 淡出效果
function fadeOut(element, duration) {
    var start = performance.now();

    requestAnimationFrame(function fade() {
        var now = performance.now();
        var elapsed = now - start;
        var progress = elapsed / duration;

        element.style.opacity = Math.max(1 - progress, 0);

        if (progress < 1) {
            requestAnimationFrame(fade);
        } else {
            element.style.display = 'none';
        }
    });
}

// 使用示例
var element = document.getElementById('myElement');

fadeIn(element, 1000); // 淡入效果,持续1秒
fadeOut(element, 1000); // 淡出效果,持续1秒
Copy after login

2. Panning effect
The panning effect allows elements to move in the web page and create a smooth animation effect. The following is an example of JavaScript code to achieve a panning effect:

// 平移效果
function move(element, targetX, targetY, duration) {
    var startX = element.offsetLeft;
    var startY = element.offsetTop;
    var distanceX = targetX - startX;
    var distanceY = targetY - startY;

    var start = performance.now();

    requestAnimationFrame(function step(timestamp) {
        var elapsed = timestamp - start;
        var progress = elapsed / duration;

        element.style.left = startX + (distanceX * progress) + 'px';
        element.style.top = startY + (distanceY * progress) + 'px';

        if (progress < 1) {
            requestAnimationFrame(step);
        }
    });
}

// 使用示例
var element = document.getElementById('myElement');

move(element, 200, 200, 1000); // 在1秒内将元素平移到坐标(200, 200)
Copy after login

3. Zoom effect
The zoom effect can change the size of elements and produce cool visual effects. The following is a JavaScript code example to achieve a zoom effect:

// 缩放效果
function scale(element, targetScale, duration) {
    var startScale = 1;
    var deltaScale = targetScale - startScale;

    var start = performance.now();

    requestAnimationFrame(function step(timestamp) {
        var elapsed = timestamp - start;
        var progress = elapsed / duration;

        element.style.transform = 'scale(' + (startScale + (deltaScale * progress)) + ')';

        if (progress < 1) {
            requestAnimationFrame(step);
        }
    });
}

// 使用示例
var element = document.getElementById('myElement');

scale(element, 2, 1000); // 在1秒内将元素放大为原来的两倍
Copy after login

The above are several common JavaScript code examples for animation effects and transition effects. By learning and using these codes, it can help us better master animation and transition effects in JavaScript, adding more fun and creativity to web design and development. Hope this article is helpful to everyone!

The above is the detailed content of Learn animations and transitions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!