当前位置:首页 > JavaScript

js鼠标事件实现动效

2026-01-31 08:56:43JavaScript

鼠标事件基础

在JavaScript中,常见的鼠标事件包括clickmouseovermouseoutmousemovemousedownmouseup。通过监听这些事件,可以触发不同的动效。

element.addEventListener('mouseover', () => {
  element.style.transform = 'scale(1.1)';
});

鼠标悬停缩放效果

通过mouseovermouseout事件实现悬停缩放效果。结合CSS的transition属性使动画更平滑。

const box = document.querySelector('.box');
box.addEventListener('mouseover', () => {
  box.style.transform = 'scale(1.2)';
  box.style.transition = 'transform 0.3s ease';
});
box.addEventListener('mouseout', () => {
  box.style.transform = 'scale(1)';
});

鼠标移动跟随效果

利用mousemove事件获取鼠标位置,动态调整元素的位置或样式。

document.addEventListener('mousemove', (e) => {
  const follower = document.querySelector('.follower');
  follower.style.left = `${e.clientX}px`;
  follower.style.top = `${e.clientY}px`;
});

鼠标点击涟漪效果

通过mousedown事件触发涟漪动画,动态创建元素并添加CSS动画。

js鼠标事件实现动效

const button = document.querySelector('.ripple-button');
button.addEventListener('mousedown', (e) => {
  const ripple = document.createElement('span');
  ripple.classList.add('ripple');
  ripple.style.left = `${e.offsetX}px`;
  ripple.style.top = `${e.offsetY}px`;
  button.appendChild(ripple);
  setTimeout(() => ripple.remove(), 600);
});

鼠标拖拽效果

结合mousedownmousemovemouseup事件实现拖拽功能。

const draggable = document.querySelector('.draggable');
let isDragging = false;
let offsetX, offsetY;

draggable.addEventListener('mousedown', (e) => {
  isDragging = true;
  offsetX = e.clientX - draggable.getBoundingClientRect().left;
  offsetY = e.clientY - draggable.getBoundingClientRect().top;
});

document.addEventListener('mousemove', (e) => {
  if (!isDragging) return;
  draggable.style.left = `${e.clientX - offsetX}px`;
  draggable.style.top = `${e.clientY - offsetY}px`;
});

document.addEventListener('mouseup', () => {
  isDragging = false;
});

鼠标悬停颜色渐变

通过mouseovermouseout事件结合CSS渐变实现颜色过渡。

const card = document.querySelector('.card');
card.addEventListener('mouseover', () => {
  card.style.background = 'linear-gradient(to right, #ff7e5f, #feb47b)';
});
card.addEventListener('mouseout', () => {
  card.style.background = 'linear-gradient(to right, #feb47b, #ff7e5f)';
});

鼠标悬停3D翻转

利用CSS的transformperspective属性,结合鼠标事件实现3D翻转效果。

js鼠标事件实现动效

const flipCard = document.querySelector('.flip-card');
flipCard.addEventListener('mouseover', () => {
  flipCard.style.transform = 'rotateY(180deg)';
  flipCard.style.transition = 'transform 0.6s';
});
flipCard.addEventListener('mouseout', () => {
  flipCard.style.transform = 'rotateY(0deg)';
});

鼠标移动视差效果

通过mousemove事件计算鼠标位置,调整不同图层的移动速度,实现视差效果。

document.addEventListener('mousemove', (e) => {
  const parallaxLayers = document.querySelectorAll('.parallax-layer');
  parallaxLayers.forEach(layer => {
    const speed = layer.getAttribute('data-speed');
    const x = (window.innerWidth - e.pageX * speed) / 100;
    const y = (window.innerHeight - e.pageY * speed) / 100;
    layer.style.transform = `translateX(${x}px) translateY(${y}px)`;
  });
});

鼠标悬停文字阴影

通过mouseovermouseout事件动态添加或移除文字阴影效果。

const text = document.querySelector('.hover-text');
text.addEventListener('mouseover', () => {
  text.style.textShadow = '2px 2px 4px rgba(0, 0, 0, 0.5)';
});
text.addEventListener('mouseout', () => {
  text.style.textShadow = 'none';
});

鼠标悬停SVG路径动画

针对SVG元素,通过鼠标事件触发路径动画或填充变化。

const svgPath = document.querySelector('.svg-path');
svgPath.addEventListener('mouseover', () => {
  svgPath.style.strokeDashoffset = '0';
  svgPath.style.transition = 'stroke-dashoffset 1s ease-in-out';
});
svgPath.addEventListener('mouseout', () => {
  svgPath.style.strokeDashoffset = '1000';
});

标签: 鼠标事件
分享给朋友:

相关文章

uniapp事件介绍

uniapp事件介绍

uniapp事件介绍 uniapp中的事件系统基于Vue.js的事件机制,允许开发者在组件间进行通信或响应用户交互。以下是uniapp中常见的事件类型和使用方法。 事件绑定与触发 在uniapp中…

vue实现事件监听

vue实现事件监听

Vue 事件监听实现方法 Vue 提供了多种方式实现事件监听,涵盖组件内外交互、原生 DOM 事件处理等场景。以下是核心实现方案: 模板内直接监听 通过 v-on 或 @ 语法监听 DOM 事件:…

vue实现change事件

vue实现change事件

Vue 实现 change 事件的方法 在 Vue 中,可以通过多种方式监听和处理 change 事件,以下是常见的实现方法: 使用 v-on 或 @ 语法监听 change 事件 <t…

vue实现鼠标框选

vue实现鼠标框选

Vue 实现鼠标框选功能 基本思路 鼠标框选功能通常需要监听鼠标的按下、移动和释放事件,记录起始点和当前位置,动态计算选区范围并高亮显示。在Vue中可以通过指令或组件封装实现。 核心实现步骤 模板部…

vue事件如何实现

vue事件如何实现

Vue 事件实现方法 Vue 中事件通过 v-on 指令或 @ 简写实现,支持原生 DOM 事件和自定义事件。 原生 DOM 事件绑定 <button v-on:click="handleCl…

vue实现全选事件

vue实现全选事件

Vue 实现全选事件 在 Vue 中实现全选功能通常涉及以下关键步骤: 数据绑定与状态管理 使用 v-model 绑定复选框的状态,确保数据与视图同步。通常需要一个数组存储选中项和一个布尔值控制全…