当前位置:首页 > 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!');

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

相关文章

vue定时器简单实现

vue定时器简单实现

使用 setTimeout 实现单次定时任务 在 Vue 中可以通过 setTimeout 实现单次延迟执行。以下是一个简单示例: export default { methods: {…

vue实现简单拖拽

vue实现简单拖拽

Vue 实现简单拖拽功能 在 Vue 中实现拖拽功能可以通过 HTML5 的拖放 API 或第三方库(如 vuedraggable)来完成。以下是两种常见的实现方式: 使用 HTML5 拖放 API…

vue修改简单实现

vue修改简单实现

Vue 修改简单实现 在 Vue 中实现简单的修改操作,通常涉及数据绑定、方法调用和响应式更新。以下是几种常见场景的实现方式: 修改数据并更新视图 Vue 的响应式系统会自动追踪数据变化并更新视图。…

vue实现好看的特效

vue实现好看的特效

Vue实现视觉特效的常用方法 使用CSS动画与过渡 Vue内置的<transition>和<transition-group>组件可轻松实现元素出现/消失的动画效果。通过组合C…

vue实现简单登录功能

vue实现简单登录功能

实现登录功能的基本步骤 安装Vue及相关依赖,确保项目环境配置正确。使用Vue CLI创建项目或直接在现有项目中集成Vue。 创建登录表单组件,包含用户名和密码输入框。使用v-model实现双向数据…

vue简单的列表实现

vue简单的列表实现

实现简单的 Vue 列表 在 Vue 中实现列表功能通常使用 v-for 指令。以下是一个基础的列表实现示例: <template> <div> <ul&g…