&…">
当前位置:首页 > 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 } } } } }

```

无缝循环滚动

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

vue实现广告滚动效果

<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控制来实现更灵活的效果。

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

相关文章

h5所能实现的效果

h5所能实现的效果

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

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue实现卡片效果

vue实现卡片效果

Vue实现卡片效果的方法 使用Vue实现卡片效果可以通过多种方式完成,包括使用原生CSS、UI框架或自定义组件。以下是几种常见方法: 使用原生CSS和Vue组件 创建一个简单的卡片组件,结合CSS实…

vue实现聊天效果

vue实现聊天效果

Vue 实现聊天效果 在 Vue 中实现聊天效果,可以通过组件化设计、数据绑定和事件处理来完成。以下是关键步骤和代码示例。 数据结构和状态管理 使用 Vue 的 data 或 ref(Composi…