<…">
当前位置:首页 > JavaScript

js实现流光

2026-04-05 07:53:07JavaScript

js实现流光

js实现流光

流光效果实现方法

通过CSS动画和JavaScript动态控制实现元素流光效果,以下是两种常见实现方式:

CSS动画实现基础流光

<div class="light-flow"></div>
.light-flow {
  width: 200px;
  height: 50px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.8), transparent);
  position: relative;
  overflow: hidden;
}

.light-flow::before {
  content: "";
  position: absolute;
  width: 50%;
  height: 100%;
  background: linear-gradient(90deg, transparent, white, transparent);
  animation: flow 2s linear infinite;
}

@keyframes flow {
  0% { left: -50%; }
  100% { left: 100%; }
}

JavaScript动态控制流光

<button id="flow-btn">流光按钮</button>
const btn = document.getElementById('flow-btn');
btn.addEventListener('mouseenter', () => {
  btn.style.position = 'relative';
  btn.style.overflow = 'hidden';

  const flow = document.createElement('div');
  flow.style.position = 'absolute';
  flow.style.width = '20px';
  flow.style.height = '200%';
  flow.style.background = 'linear-gradient(90deg, transparent, rgba(255,255,255,0.7), transparent)';
  flow.style.transform = 'rotate(30deg)';
  flow.style.left = '-20px';
  flow.style.top = '-50%';
  flow.style.animation = 'flow 1.5s linear';

  btn.appendChild(flow);

  setTimeout(() => {
    flow.remove();
  }, 1500);
});

// 添加CSS
const style = document.createElement('style');
style.textContent = `
  @keyframes flow {
    0% { left: -20px; }
    100% { left: calc(100% + 20px); }
  }
`;
document.head.appendChild(style);

高级流光效果实现

跟随鼠标的流光效果

<div id="flow-container"></div>
const container = document.getElementById('flow-container');
container.style.position = 'relative';
container.style.width = '100%';
container.style.height = '300px';
container.style.background = '#222';

container.addEventListener('mousemove', (e) => {
  const flow = document.createElement('div');
  flow.style.position = 'absolute';
  flow.style.width = '100px';
  flow.style.height = '100px';
  flow.style.borderRadius = '50%';
  flow.style.background = 'radial-gradient(circle, rgba(255,255,255,0.8) 0%, transparent 70%)';
  flow.style.left = `${e.clientX - 50}px`;
  flow.style.top = `${e.clientY - 50}px`;
  flow.style.opacity = '1';
  flow.style.transition = 'opacity 0.5s ease-out';

  container.appendChild(flow);

  setTimeout(() => {
    flow.style.opacity = '0';
    setTimeout(() => flow.remove(), 500);
  }, 100);
});

SVG实现复杂流光

<svg id="svg-flow" width="300" height="100">
  <defs>
    <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="transparent" />
      <stop offset="50%" stop-color="white" />
      <stop offset="100%" stop-color="transparent" />
    </linearGradient>
  </defs>
  <rect id="flow-rect" x="0" y="0" width="100%" height="100%" fill="url(#gradient)" />
</svg>
const rect = document.getElementById('flow-rect');
let pos = -300;

function animate() {
  pos = (pos > 600) ? -300 : pos + 2;
  rect.setAttribute('x', pos);
  requestAnimationFrame(animate);
}

animate();

性能优化建议

  1. 使用will-change属性提前告知浏览器元素将发生变化
  2. 合理使用requestAnimationFrame代替setInterval
  3. 对频繁创建销毁的元素使用对象池技术
  4. 避免过多使用滤镜效果(filter)
  5. 对静态流光效果优先使用CSS实现

以上方法可根据实际需求组合使用,通过调整颜色、速度、尺寸等参数可获得不同风格的流光效果。

标签: 流光js
分享给朋友:

相关文章

js实现轮播图

js实现轮播图

轮播图实现方法 基础HTML结构 <div class="slider"> <div class="slides"> <img src="image1.jpg…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…