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

vue实现广告横幅

2026-03-28 19:43:31VUE

vue实现广告横幅

Vue实现广告横幅的方法

使用v-if/v-show控制显示

<template>
  <div v-if="showBanner" class="ad-banner">
    <span @click="closeBanner">×</span>
    <img src="@/assets/ad.jpg" alt="广告">
  </div>
</template>

<script>
export default {
  data() {
    return {
      showBanner: true
    }
  },
  methods: {
    closeBanner() {
      this.showBanner = false
    }
  }
}
</script>

<style scoped>
.ad-banner {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 80px;
  background: #f5f5f5;
  text-align: center;
}
.ad-banner span {
  position: absolute;
  right: 10px;
  top: 5px;
  cursor: pointer;
}
</style>

轮播广告实现

<template>
  <div class="carousel">
    <div v-for="(ad, index) in ads" 
         :key="index"
         v-show="currentIndex === index">
      <img :src="ad.image" :alt="ad.title">
    </div>
    <button @click="prev">上一张</button>
    <button @click="next">下一张</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ads: [
        { image: '@/assets/ad1.jpg', title: '广告1' },
        { image: '@/assets/ad2.jpg', title: '广告2' },
        { image: '@/assets/ad3.jpg', title: '广告3' }
      ],
      currentIndex: 0,
      timer: null
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.next()
      }, 3000)
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.ads.length) % this.ads.length
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.ads.length
    }
  }
}
</script>

使用第三方组件

安装vue-awesome-swiper:

vue实现广告横幅

npm install swiper vue-awesome-swiper

实现代码:

<template>
  <swiper :options="swiperOptions">
    <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>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      ads: [
        { image: '@/assets/ad1.jpg', title: '广告1' },
        { image: '@/assets/ad2.jpg', title: '广告2' },
        { image: '@/assets/ad3.jpg', title: '广告3' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

响应式广告实现

<template>
  <div class="responsive-ad">
    <img v-if="isMobile" src="@/assets/ad-mobile.jpg" alt="移动端广告">
    <img v-else src="@/assets/ad-desktop.jpg" alt="桌面端广告">
  </div>
</template>

<script>
export default {
  data() {
    return {
      isMobile: false
    }
  },
  mounted() {
    this.checkScreenSize()
    window.addEventListener('resize', this.checkScreenSize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkScreenSize)
  },
  methods: {
    checkScreenSize() {
      this.isMobile = window.innerWidth < 768
    }
  }
}
</script>

标签: 横幅广告
分享给朋友:

相关文章

vue实现弹出广告

vue实现弹出广告

Vue实现弹出广告的方法 在Vue中实现弹出广告可以通过多种方式完成,以下是几种常见的方法: 使用v-if/v-show控制显示 通过数据绑定控制广告的显示与隐藏。在Vue组件中定义一个布尔值数据…

vue实现漂浮广告

vue实现漂浮广告

实现漂浮广告的基本思路 在Vue中实现漂浮广告可以通过动态样式绑定和定时器控制广告元素的位置。核心是利用CSS的position: fixed或absolute定位,结合JavaScript修改top…

js广告实现

js广告实现

广告实现的基本方法 在JavaScript中实现广告功能通常涉及动态加载广告内容、跟踪用户交互以及优化广告展示效果。以下是几种常见的实现方式: 使用第三方广告平台API 大多数广告系统如Google…

js实现广告轮播的实现

js实现广告轮播的实现

广告轮播的实现方法 广告轮播(Carousel)是网页中常见的动态展示多张广告或图片的组件。以下通过纯JavaScript实现一个基础的广告轮播功能。 HTML结构 创建一个基本的HTML结构,包含…

js实现广告拦截

js实现广告拦截

广告拦截的实现方法 在JavaScript中实现广告拦截可以通过多种方式,以下是一些常见的方法: 基于DOM元素拦截 通过识别广告元素的特定类名、ID或属性,从DOM中移除这些元素。例如:…

uniapp广告弹窗

uniapp广告弹窗

uniapp广告弹窗的实现方法 在uniapp中实现广告弹窗功能,可以通过原生广告组件或自定义弹窗组件完成。以下是具体实现方案: 原生广告组件(适用于App平台) uniapp提供了<ad&g…