当前位置:首页 > JavaScript

js实现跑马灯效果

2026-01-31 08:48:56JavaScript

使用CSS动画实现跑马灯

通过CSS的@keyframesanimation属性实现文字横向滚动效果,适用于简单场景:

<div class="marquee">
  <span>这里是需要滚动的文字内容...</span>
</div>

<style>
.marquee {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}
.marquee span {
  display: inline-block;
  animation: marquee 10s linear infinite;
}
@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
</style>

使用JavaScript动态计算

通过定时器动态修改元素位置,实现更灵活的控制:

js实现跑马灯效果

function startMarquee(element, speed = 1) {
  const container = element.parentNode;
  const containerWidth = container.offsetWidth;
  let position = containerWidth;

  function animate() {
    position -= speed;
    if (position < -element.offsetWidth) {
      position = containerWidth;
    }
    element.style.transform = `translateX(${position}px)`;
    requestAnimationFrame(animate);
  }

  animate();
}

// 使用示例
const marqueeText = document.querySelector('.marquee span');
startMarquee(marqueeText, 2);

无缝循环跑马灯

克隆内容元素实现无缝滚动效果:

js实现跑马灯效果

<div class="marquee-container">
  <div class="marquee-content">需要循环滚动的内容</div>
</div>

<script>
function createSeamlessMarquee() {
  const container = document.querySelector('.marquee-container');
  const content = document.querySelector('.marquee-content');
  const clone = content.cloneNode(true);
  container.appendChild(clone);

  let position = 0;
  const speed = 1;
  const contentWidth = content.scrollWidth;

  function animate() {
    position -= speed;
    if (position <= -contentWidth) {
      position = 0;
    }
    container.style.transform = `translateX(${position}px)`;
    requestAnimationFrame(animate);
  }

  animate();
}
</script>

响应式跑马灯

结合ResizeObserver实现窗口大小变化时的自适应:

class Marquee {
  constructor(element) {
    this.element = element;
    this.content = element.querySelector('.marquee-content');
    this.clone = this.content.cloneNode(true);
    this.element.appendChild(this.clone);
    this.observer = new ResizeObserver(this.update.bind(this));
    this.observer.observe(this.element);
    this.animate();
  }

  update() {
    this.contentWidth = this.content.scrollWidth;
    this.position = 0;
  }

  animate() {
    this.position -= 1;
    if (this.position <= -this.contentWidth) {
      this.position = 0;
    }
    this.element.style.transform = `translateX(${this.position}px)`;
    requestAnimationFrame(this.animate.bind(this));
  }
}

new Marquee(document.querySelector('.marquee-container'));

带暂停控制的跑马灯

添加鼠标交互控制功能:

class InteractiveMarquee {
  constructor(element) {
    this.element = element;
    this.content = element.innerHTML;
    this.speed = 2;
    this.isPaused = false;
    this.position = element.offsetWidth;
    this.element.innerHTML = this.content + this.content;

    element.addEventListener('mouseenter', () => this.isPaused = true);
    element.addEventListener('mouseleave', () => this.isPaused = false);

    this.animate();
  }

  animate() {
    if (!this.isPaused) {
      this.position -= this.speed;
      if (this.position <= -this.element.scrollWidth/2) {
        this.position = 0;
      }
      this.element.style.transform = `translateX(${this.position}px)`;
    }
    requestAnimationFrame(this.animate.bind(this));
  }
}

new InteractiveMarquee(document.querySelector('.marquee'));

每种实现方式适用于不同场景,CSS动画方案性能最佳但控制灵活性较低,JavaScript方案可以实现更复杂的交互效果。无缝循环方案适合内容长度不固定的场景,响应式方案能更好地适应不同屏幕尺寸。

标签: 跑马灯效果
分享给朋友:

相关文章

css制作卷边效果

css制作卷边效果

使用伪元素和渐变实现卷边效果 通过CSS伪元素和线性渐变可以模拟纸张卷边的视觉效果。这种方法不需要额外元素,仅通过::before或::after伪元素实现。 .element { positi…

vue实现放大效果

vue实现放大效果

使用 CSS 过渡实现放大效果 通过 Vue 的 v-bind:class 或 v-bind:style 动态绑定 CSS 类或样式,结合 CSS 的 transform: scale() 实现平滑过…

jquery效果

jquery效果

jQuery 动画效果 jQuery 提供了多种内置动画效果,可用于实现平滑的页面交互。常用的方法包括 show()、hide()、toggle()、fadeIn()、fadeOut()、slideU…

h5所能实现的效果

h5所能实现的效果

H5能实现的效果 H5(HTML5)作为最新的HTML标准,具备强大的功能和丰富的API支持,能够实现多种现代Web应用效果。以下是H5的主要能力分类: 多媒体支持 H5原生支持音频和视频播放,无…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

uniapp 卡片效果

uniapp 卡片效果

uniapp 实现卡片效果的方法 使用 view 和 CSS 样式 通过 view 组件结合 CSS 样式可以快速实现卡片效果。设置圆角、阴影和边距来增强视觉层次感。 <view class…