当前位置:首页 > VUE

vue实现滚动广告

2026-02-19 23:07:35VUE

使用Vue实现滚动广告

滚动广告可以通过多种方式实现,以下是几种常见的方法:

使用CSS动画实现滚动

通过CSS的@keyframesanimation属性可以实现简单的滚动效果。在Vue中,可以将这些样式动态绑定到元素上。

<template>
  <div class="scroll-container">
    <div class="scroll-content" :style="{ animation: `scroll ${duration}s linear infinite` }">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '这里是滚动广告内容',
      duration: 10
    }
  }
}
</script>

<style>
.scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.scroll-content {
  display: inline-block;
  animation: scroll 10s linear infinite;
}

@keyframes scroll {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
</style>

使用JavaScript实现动态滚动

通过JavaScript动态改变元素的位置,可以实现更灵活的滚动效果。

<template>
  <div class="scroll-container" ref="container">
    <div class="scroll-content" ref="content">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '这里是滚动广告内容',
      speed: 2,
      animationId: null
    }
  },
  mounted() {
    this.startScroll()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    startScroll() {
      const container = this.$refs.container
      const content = this.$refs.content
      let position = container.offsetWidth

      const scroll = () => {
        position -= this.speed
        if (position < -content.offsetWidth) {
          position = container.offsetWidth
        }
        content.style.transform = `translateX(${position}px)`
        this.animationId = requestAnimationFrame(scroll)
      }

      scroll()
    }
  }
}
</script>

<style>
.scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.scroll-content {
  display: inline-block;
}
</style>

使用第三方库实现复杂效果

如果需要更复杂的滚动效果,可以考虑使用第三方库如vue-awesome-swiper

安装依赖:

npm install swiper vue-awesome-swiper

使用示例:

vue实现滚动广告

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(item, index) in items" :key="index">
      {{ item }}
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      items: ['广告1', '广告2', '广告3'],
      swiperOptions: {
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

注意事项

  • 对于移动端,需要考虑触摸事件的处理。
  • 滚动速度和时间需要根据内容长度调整。
  • 使用CSS动画性能较好,但灵活性较低。
  • 使用JavaScript实现可以更精确控制,但需要注意性能优化。

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

相关文章

vue实现弹窗

vue实现弹窗

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

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…

vue实现文章滚动

vue实现文章滚动

Vue 实现文章滚动效果 使用 CSS 实现基础滚动 通过 CSS 的 overflow 和 height 属性控制滚动区域,适用于静态内容滚动。在 Vue 模板中定义固定高度的容器,内容超出后自动滚…

vue实现动态禁用

vue实现动态禁用

动态禁用表单元素或按钮 在Vue中实现动态禁用功能通常通过v-bind:disabled(或简写为:disabled)绑定一个响应式变量实现。当变量值为true时,元素被禁用;为false时启用。…

vue实现项目依赖

vue实现项目依赖

Vue 项目依赖的实现方法 使用 npm 或 yarn 安装依赖 通过 npm 或 yarn 安装项目所需的依赖包。在项目根目录下运行以下命令安装 Vue 及相关库: npm install vue…

vue实现人脸比对

vue实现人脸比对

Vue 实现人脸比对的方法 使用 TensorFlow.js 和 Face-api.js 安装必要的依赖库: npm install face-api.js tensorflow.js 在 Vue…