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

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

相关文章

vue 实现拖拽事件

vue 实现拖拽事件

实现拖拽事件的基本步骤 在Vue中实现拖拽功能可以通过HTML5的Drag and Drop API或第三方库如vuedraggable来完成。以下是两种方法的详细说明。 使用HTML5 Drag…

vue实现全选事件

vue实现全选事件

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

vue 事件实现原理

vue 事件实现原理

Vue 事件实现原理 Vue 的事件系统基于发布-订阅模式,通过 $on、$emit 等方法实现组件间通信。核心原理分为以下几个部分: 事件注册与监听 Vue 实例内部维护一个 _events 对象…

vue实现鼠标的监听

vue实现鼠标的监听

Vue 中监听鼠标事件的方法 在 Vue 中可以通过多种方式监听鼠标事件,以下是常见的实现方法: 使用 v-on 指令 Vue 提供了 v-on 指令(简写为 @)来监听 DOM 事件,包括鼠标事件…

vue中实现长按事件

vue中实现长按事件

监听原生事件实现长按 在Vue中可以通过@mousedown和@touchstart绑定原生事件,配合setTimeout触发长按逻辑。清除定时器使用@mouseup、@mouseleave和@tou…

vue实现鼠标移入事件

vue实现鼠标移入事件

鼠标移入事件的基本实现 在Vue中实现鼠标移入事件可以通过v-on指令或@简写绑定mouseenter或mouseover事件。两者的区别在于事件冒泡机制: mouseenter:不会冒泡,仅在鼠标…