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

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

相关文章

js实现广告轮播的实现

js实现广告轮播的实现

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

js广告弹窗实现

js广告弹窗实现

基础弹窗实现 使用alert()函数可以快速创建简单的弹窗: alert("这是一个基础弹窗"); 自定义弹窗样式 通过HTML+CSS创建更美观的弹窗: <div id="customPo…

uniapp关闭广告

uniapp关闭广告

关闭广告的方法 在UniApp中关闭广告通常涉及多个层面,包括开发配置、第三方广告平台设置以及用户端操作。以下是具体方法: 开发端配置 检查项目中是否集成了广告模块(如穿山甲、广点通等),在mani…

uniapp打包有广告

uniapp打包有广告

uniapp打包去除广告的方法 检查manifest.json配置 在项目根目录下的manifest.json文件中,确保没有启用广告模块。找到"app-plus"或"mp-weixin"等平台配置,…

uniapp官网广告

uniapp官网广告

uniapp官网广告相关说明 DCloud(数字天堂)作为uniapp的官方开发团队,其官网(https://uniapp.dcloud.net.cn)会展示部分广告内容,主要包括以下类型: DC…

vue实现广告组件

vue实现广告组件

Vue 广告组件实现方案 基础组件结构 创建一个可复用的广告组件,使用单文件组件形式(.vue)。核心结构包括模板、脚本和样式部分。 <template> <div class…