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

实现打字机效果

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

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 实现简单特效

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

相关文章

vue实现简单表格

vue实现简单表格

Vue 实现简单表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 原生表格标签,通过 v-for 动态渲染数据: <template> <tabl…

vue实现特效

vue实现特效

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

vue实现简单搜索

vue实现简单搜索

Vue 实现简单搜索功能 数据绑定与输入框 在 Vue 中实现搜索功能,通常使用 v-model 绑定输入框的值到数据属性。创建一个输入框用于用户输入搜索关键词: <template>…

vue实现滚轮特效

vue实现滚轮特效

vue实现滚轮特效的方法 使用原生事件监听 在Vue组件中通过@wheel事件监听滚轮动作,结合CSS transform属性实现缩放或位移效果。示例代码: <template>…

vue 实现简单表格

vue 实现简单表格

以下是在 Vue 中实现简单表格的几种方法: 使用原生表格标签 通过 Vue 的数据绑定功能渲染表格数据: <template> <table> <the…

vue实现交互特效

vue实现交互特效

Vue 实现交互特效的方法 Vue.js 提供了丰富的工具和指令,可以轻松实现各种交互特效。以下是几种常见的实现方式: 使用 Vue 过渡和动画 Vue 的 <transition> 和…