当前位置:首页 > JavaScript

js实现阻尼

2026-03-14 13:04:20JavaScript

阻尼效果的实现方法

阻尼效果常用于实现平滑滚动或缓动动画,以下是几种常见的实现方式:

js实现阻尼

使用requestAnimationFrame实现基础阻尼

function dampenAnimation(target, current, velocity, damping, deltaTime) {
    const difference = target - current;
    velocity += difference * deltaTime;
    velocity *= damping;
    return current + velocity;
}

let position = 0;
let velocity = 0;
const target = 100;
const damping = 0.8;

function animate() {
    position = dampenAnimation(target, position, velocity, damping, 0.016);
    console.log(position);

    if (Math.abs(position - target) > 0.1) {
        requestAnimationFrame(animate);
    }
}
animate();

滚动阻尼效果

为网页滚动添加阻尼效果:

js实现阻尼

let startY = 0;
let currentY = 0;
let targetY = 0;
let velocity = 0;
const damping = 0.2;

document.addEventListener('wheel', (e) => {
    e.preventDefault();
    targetY += e.deltaY;
});

function animateScroll() {
    const diff = targetY - currentY;
    velocity += diff * 0.1;
    velocity *= damping;
    currentY += velocity;

    window.scrollTo(0, currentY);
    requestAnimationFrame(animateScroll);
}
animateScroll();

物理引擎式阻尼

更接近真实物理的阻尼实现:

class Dampener {
    constructor(mass = 1, damping = 0.5, stiffness = 100) {
        this.mass = mass;
        this.damping = damping;
        this.stiffness = stiffness;
        this.position = 0;
        this.velocity = 0;
    }

    update(target, deltaTime) {
        const springForce = -this.stiffness * (this.position - target);
        const dampingForce = -this.damping * this.velocity;
        const acceleration = (springForce + dampingForce) / this.mass;

        this.velocity += acceleration * deltaTime;
        this.position += this.velocity * deltaTime;

        return this.position;
    }
}

应用阻尼到DOM元素

为DOM元素移动添加阻尼效果:

const element = document.getElementById('box');
const dampener = new Dampener();

function moveTo(x, y) {
    const deltaTime = 0.016; // 约60fps
    const currentX = dampener.update(x, deltaTime);
    const currentY = dampener.update(y, deltaTime);

    element.style.transform = `translate(${currentX}px, ${currentY}px)`;

    if (Math.abs(dampener.velocity) > 0.01) {
        requestAnimationFrame(() => moveTo(x, y));
    }
}

moveTo(200, 200);

参数调整建议

  • 增大damping值会更快停止运动
  • 减小stiffness会使运动更柔软
  • mass影响运动的惯性效果
  • deltaTime应接近实际帧间隔时间

这些实现可以根据具体需求调整参数,适用于滚动效果、动画过渡或物理模拟等场景。

标签: 阻尼js
分享给朋友:

相关文章

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Java…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js画图实现

js画图实现

使用Canvas API绘制图形 Canvas是HTML5提供的绘图API,通过JavaScript操作Canvas元素可以绘制各种图形。以下是一个简单的示例: <canvas id="myC…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现路由

js实现路由

js实现路由的方法 在JavaScript中实现路由功能可以通过多种方式完成,以下是几种常见的方法: 使用原生JavaScript实现路由 通过监听window.onhashchange事件来实现基…