当前位置:首页 > JavaScript

js 实现简单特效

2026-02-02 22:05:23JavaScript

实现鼠标悬停按钮变色

通过监听鼠标事件改变按钮背景色,适合增强交互体验。以下代码使用纯JavaScript实现:

const button = document.getElementById('hoverButton');
button.addEventListener('mouseover', () => {
  button.style.backgroundColor = '#ff5722';
  button.style.transition = 'background-color 0.3s ease';
});
button.addEventListener('mouseout', () => {
  button.style.backgroundColor = '#4CAF50';
});

创建元素淡入效果

使用CSS配合JavaScript实现平滑的淡入动画效果:

function fadeIn(element) {
  element.style.opacity = 0;
  element.style.display = 'block';
  let opacity = 0;
  const timer = setInterval(() => {
    opacity += 0.1;
    element.style.opacity = opacity;
    if (opacity >= 1) clearInterval(timer);
  }, 50);
}
// 调用示例:fadeIn(document.getElementById('target'));

实现滚动动画特效

监听滚动事件创建视差效果或元素显示动画:

window.addEventListener('scroll', () => {
  const elements = document.querySelectorAll('.animate-on-scroll');
  elements.forEach(el => {
    const rect = el.getBoundingClientRect();
    if (rect.top < window.innerHeight * 0.75) {
      el.classList.add('active');
    }
  });
});

需配合CSS:

.animate-on-scroll {
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.6s ease;
}
.animate-on-scroll.active {
  opacity: 1;
  transform: translateY(0);
}

制作点击波纹效果

模拟Material Design的点击波纹动画:

function createRipple(event) {
  const button = event.currentTarget;
  const circle = document.createElement('span');
  const diameter = Math.max(button.clientWidth, button.clientHeight);
  const radius = diameter / 2;

  circle.style.width = circle.style.height = `${diameter}px`;
  circle.style.left = `${event.clientX - button.offsetLeft - radius}px`;
  circle.style.top = `${event.clientY - button.offsetTop - radius}px`;
  circle.classList.add('ripple');

  const ripple = button.getElementsByClassName('ripple')[0];
  if (ripple) ripple.remove();

  button.appendChild(circle);
}

// 为元素添加点击事件:element.addEventListener('click', createRipple);

需配套CSS:

.ripple {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255,255,255,0.7);
  transform: scale(0);
  animation: ripple 0.6s linear;
  pointer-events: none;
}

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}

实现打字机效果

模拟逐字打印的文字动画:

js 实现简单特效

function typeWriter(element, text, speed = 100) {
  let i = 0;
  element.textContent = '';
  function typing() {
    if (i < text.length) {
      element.textContent += text.charAt(i);
      i++;
      setTimeout(typing, speed);
    }
  }
  typing();
}
// 使用示例:typeWriter(document.getElementById('typeElement'), 'Hello World!');

标签: 特效简单
分享给朋友:

相关文章

js多线程如何简单实现

js多线程如何简单实现

JavaScript 多线程的简单实现方法 JavaScript 本身是单线程的,但可以通过 Web Workers 实现多线程操作。Web Workers 允许在后台运行脚本,独立于主线程执行任务。…

vue实现简单表格

vue实现简单表格

使用 Vue 实现简单表格 Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法: 基础表格实现 通过 v-for 指令循环渲染表格数据: <template&…

vue实现简单登录

vue实现简单登录

使用 Vue 实现简单登录功能 创建 Vue 项目 确保已安装 Vue CLI,通过以下命令创建新项目: vue create vue-login-demo 选择默认配置或手动配置(如 Babel、…

vue怎么实现特效

vue怎么实现特效

Vue实现特效的方法 Vue可以通过多种方式实现特效,包括内置过渡动画、第三方动画库、CSS动画和JavaScript动画等。以下是几种常见的方法: 使用Vue内置过渡动画 Vue提供了<t…

vue实现各种特效

vue实现各种特效

Vue实现动画效果 使用Vue的过渡系统实现元素进入/离开的动画效果。通过<transition>组件包裹需要动画的元素,并定义CSS过渡类名或JavaScript钩子。 <tra…

vue简单实现picker效果

vue简单实现picker效果

Vue 实现 Picker 效果 在 Vue 中实现 Picker 效果可以通过自定义组件或使用第三方库完成。以下是两种常见方法: 自定义滚动选择器 使用原生滚动行为实现 Picker 效果,适用于…