当前位置:首页 > VUE

vue实现滚动广告

2026-01-19 06:45:26VUE

实现滚动广告的基本思路

在Vue中实现滚动广告通常可以通过CSS动画或JavaScript定时器控制元素的位移。核心逻辑是利用动态样式或数据绑定,周期性更新广告内容的位置或列表。

使用CSS动画实现

通过CSS的@keyframestransform实现无限滚动效果,适用于单行横向或纵向滚动:

<template>
  <div class="ad-container">
    <div class="ad-content">
      <!-- 广告内容重复两份以实现无缝滚动 -->
      <div v-for="(item, index) in ads.concat(ads)" :key="index" class="ad-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { text: '广告1' },
        { text: '广告2' },
        { text: '广告3' }
      ]
    }
  }
}
</script>

<style>
.ad-container {
  width: 100%;
  overflow: hidden;
}
.ad-content {
  display: flex;
  animation: scroll 10s linear infinite;
}
@keyframes scroll {
  0% { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}
.ad-item {
  flex-shrink: 0;
  width: 200px;
  padding: 10px;
}
</style>

使用JavaScript定时器控制

通过setInterval动态修改数据或样式,实现更灵活的交互控制:

vue实现滚动广告

<template>
  <div class="ad-wrapper" @mouseenter="pause" @mouseleave="resume">
    <div class="ad-list" :style="{ transform: `translateX(${offset}px)` }">
      <div v-for="(item, index) in ads" :key="index" class="ad-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { text: '促销活动1' },
        { text: '新品上市2' },
        { text: '限时优惠3' }
      ],
      offset: 0,
      timer: null,
      speed: 2
    }
  },
  mounted() {
    this.startScroll()
  },
  methods: {
    startScroll() {
      this.timer = setInterval(() => {
        this.offset -= this.speed
        if (Math.abs(this.offset) >= this.$el.offsetWidth) {
          this.offset = 0
        }
      }, 16)
    },
    pause() {
      clearInterval(this.timer)
    },
    resume() {
      this.startScroll()
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style>
.ad-wrapper {
  width: 100%;
  overflow: hidden;
}
.ad-list {
  display: flex;
  transition: transform 0.3s ease;
}
.ad-item {
  flex-shrink: 0;
  width: 200px;
}
</style>

使用第三方库

对于复杂需求,可考虑使用专门轮播库如vue-awesome-swiper

  1. 安装依赖:

    vue实现滚动广告

    npm install swiper vue-awesome-swiper
  2. 组件实现:

    
    <template>
    <swiper :options="swiperOption" class="ad-swiper">
     <swiper-slide v-for="(ad, index) in ads" :key="index">
       <img :src="ad.image" alt="广告">
     </swiper-slide>
    </swiper>
    </template>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper' import 'swiper/css/swiper.css'

export default { components: { Swiper, SwiperSlide }, data() { return { ads: [ { image: '/ad1.jpg' }, { image: '/ad2.jpg' } ], swiperOption: { autoplay: { delay: 3000, disableOnInteraction: false }, loop: true } } } }

.ad-swiper { height: 200px; } ```

注意事项

  • 性能优化:大量DOM操作时建议使用虚拟滚动技术
  • 响应式设计:通过CSS媒体查询适配不同屏幕尺寸
  • 内存管理:组件销毁时清除定时器
  • 无障碍访问:为滚动内容添加ARIA标签

以上方法可根据实际需求组合使用,CSS动画适合简单场景,JavaScript控制更灵活,第三方库能快速实现复杂效果。

标签: 广告vue
分享给朋友:

相关文章

vue实现选区创建

vue实现选区创建

Vue 实现选区创建的方法 在 Vue 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ran…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…