…">
当前位置:首页 > VUE

vue实现广告滚动效果

2026-01-22 04:59:54VUE

Vue实现广告滚动效果

横向滚动效果

使用CSS动画和Vue结合实现横向滚动广告:

<template>
  <div class="ad-container">
    <div class="ad-content" :style="{ animationDuration: duration + 's' }">
      <div v-for="(ad, index) in ads" :key="index" class="ad-item">
        <img :src="ad.image" :alt="ad.title">
        <p>{{ ad.title }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { image: 'ad1.jpg', title: '广告1' },
        { image: 'ad2.jpg', title: '广告2' },
        { image: 'ad3.jpg', title: '广告3' }
      ],
      duration: 10
    }
  }
}
</script>

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

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

.ad-item {
  display: inline-block;
  width: 200px;
  margin-right: 20px;
}

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

纵向滚动效果

实现垂直方向滚动的广告栏:

<template>
  <div class="vertical-ad" @mouseenter="stopScroll" @mouseleave="startScroll">
    <ul ref="adList">
      <li v-for="(item, index) in ads" :key="index">
        {{ item.text }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { text: '广告内容1' },
        { text: '广告内容2' },
        { text: '广告内容3' }
      ],
      timer: null,
      currentIndex: 0
    }
  },
  mounted() {
    this.startScroll();
  },
  methods: {
    startScroll() {
      this.timer = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex >= this.ads.length) {
          this.currentIndex = 0;
        }
        this.$refs.adList.style.transform = `translateY(-${this.currentIndex * 30}px)`;
      }, 2000);
    },
    stopScroll() {
      clearInterval(this.timer);
    }
  }
}
</script>

<style scoped>
.vertical-ad {
  height: 30px;
  overflow: hidden;
  position: relative;
}

ul {
  transition: transform 0.5s ease;
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  height: 30px;
  line-height: 30px;
}
</style>

使用第三方组件

对于更复杂的需求,可以考虑使用现成的Vue轮播组件:

  1. 安装vue-awesome-swiper

    npm install swiper vue-awesome-swiper --save
  2. 实现自动轮播广告

    
    <template>
    <swiper :options="swiperOption">
     <swiper-slide v-for="(ad, index) in ads" :key="index">
       <img :src="ad.image" :alt="ad.title">
     </swiper-slide>
     <div class="swiper-pagination" slot="pagination"></div>
    </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', title: '广告1' }, { image: 'ad2.jpg', title: '广告2' }, { image: 'ad3.jpg', title: '广告3' } ], swiperOption: { autoplay: { delay: 2500, disableOnInteraction: false }, pagination: { el: '.swiper-pagination', clickable: true } } } } }

```

无缝循环滚动

实现无限循环的无缝滚动效果:

<template>
  <div class="seamless-ad" ref="container">
    <div class="ad-wrapper" ref="wrapper">
      <div v-for="(item, index) in ads" :key="index" class="ad-item">
        {{ item.text }}
      </div>
      <!-- 复制一份实现无缝效果 -->
      <div v-for="(item, index) in ads" :key="index + 'copy'" class="ad-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { text: '滚动广告1' },
        { text: '滚动广告2' },
        { text: '滚动广告3' }
      ],
      scrollSpeed: 1,
      animationFrame: null
    }
  },
  mounted() {
    this.startAnimation();
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationFrame);
  },
  methods: {
    startAnimation() {
      const wrapper = this.$refs.wrapper;
      const container = this.$refs.container;

      const animate = () => {
        wrapper.style.transform = `translateX(-${this.scrollSpeed}px)`;

        if (this.scrollSpeed > wrapper.scrollWidth / 2) {
          this.scrollSpeed = 0;
          wrapper.style.transform = 'translateX(0)';
        } else {
          this.scrollSpeed += 0.5;
        }

        this.animationFrame = requestAnimationFrame(animate);
      };

      animate();
    }
  }
}
</script>

<style scoped>
.seamless-ad {
  width: 100%;
  overflow: hidden;
}

.ad-wrapper {
  display: flex;
  white-space: nowrap;
  transition: transform 0.1s linear;
}

.ad-item {
  padding: 0 20px;
  flex-shrink: 0;
}
</style>

以上实现方式可根据实际需求进行调整,包括滚动速度、方向、动画效果等参数。对于更复杂的广告滚动效果,建议结合CSS动画和JavaScript控制来实现更灵活的效果。

vue实现广告滚动效果

标签: 效果广告
分享给朋友:

相关文章

vue实现拖拉效果

vue实现拖拉效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5拖拽API HTML5提供…

vue实现放大效果

vue实现放大效果

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

vue实现对话框效果

vue实现对话框效果

Vue 对话框实现方法 使用组件化方式封装对话框 创建一个独立的对话框组件(如Dialog.vue),通过v-model控制显示隐藏: <template> <div cla…

h5如何实现vr效果

h5如何实现vr效果

使用WebXR API实现VR效果 WebXR是浏览器中实现VR/AR的核心API,支持设备姿态追踪、渲染交互等功能。需在支持WebXR的设备(如Oculus、HTC Vive)或浏览器模拟环境中运行…

js实现滚动条效果

js实现滚动条效果

实现滚动条效果的方法 使用原生JavaScript实现滚动条 通过监听滚动事件,可以动态调整滚动条的位置和样式。以下是一个基础实现示例: // 获取DOM元素 const container = d…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-c…