当前位置:首页 > JavaScript

js实现特效

2026-03-15 08:40:42JavaScript

JavaScript 实现常见特效的方法

鼠标悬停效果
通过监听mouseovermouseout事件改变元素样式:

element.addEventListener('mouseover', () => {
  element.style.transform = 'scale(1.1)';
  element.style.transition = 'all 0.3s';
});
element.addEventListener('mouseout', () => {
  element.style.transform = 'scale(1)';
});

滚动动画
使用IntersectionObserver实现滚动视差效果:

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.classList.add('animate');
    }
  });
});
document.querySelectorAll('.scroll-item').forEach(item => observer.observe(item));

粒子效果
Canvas实现基础粒子动画:

const canvas = document.getElementById('particle-canvas');
const ctx = canvas.getContext('2d');
const particles = Array(100).fill().map(() => ({
  x: Math.random() * canvas.width,
  y: Math.random() * canvas.height,
  size: Math.random() * 5 + 1,
  speedX: Math.random() * 3 - 1.5,
  speedY: Math.random() * 3 - 1.5
}));

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  particles.forEach(p => {
    p.x += p.speedX;
    p.y += p.speedY;
    ctx.fillStyle = 'rgba(255,255,255,0.8)';
    ctx.beginPath();
    ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
    ctx.fill();
  });
  requestAnimationFrame(animate);
}
animate();

特效优化技巧

性能优化
使用requestAnimationFrame替代setTimeout实现动画循环,避免布局抖动(Layout Thrashing):

function optimizedAnimation() {
  const elements = document.querySelectorAll('.animate-me');
  requestAnimationFrame(() => {
    elements.forEach(el => {
      const position = el.getBoundingClientRect().top; // 批量读取
      el.style.transform = `translateY(${position * 0.1}px)`; // 批量写入
    });
  });
}

硬件加速
通过触发GPU加速提升动画流畅度:

.accelerated {
  transform: translateZ(0);
  will-change: transform;
}

特效库推荐

轻量级选择

  • Anime.js:支持复杂时间线控制的动画库(12KB)
  • GSAP:专业级动画工具集,适合复杂场景
  • Particles.js:专攻粒子效果的轻量库

实现3D特效
Three.js基础示例:

js实现特效

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;

function animate() {
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}
animate();

标签: 特效js
分享给朋友:

相关文章

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

vue实现特效

vue实现特效

Vue实现特效的方法 Vue.js可以通过多种方式实现动态特效,包括内置过渡动画、第三方动画库以及结合CSS3或JavaScript原生动画。以下是几种常见的实现方法: 使用Vue过渡系统 Vue提…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

js实现vr

js实现vr

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

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…