当前位置:首页 > VUE

vue实现开屏广告

2026-01-16 02:04:36VUE

实现开屏广告的基本思路

在Vue中实现开屏广告通常需要结合路由守卫、定时器和本地存储技术。核心逻辑包括广告显示、倒计时控制以及跳过功能。

广告页面组件设计

创建一个独立的广告组件,通常命名为SplashAd.vue

<template>
  <div class="splash-ad">
    <img :src="adImage" alt="广告" />
    <div class="skip-btn" @click="skipAd">
      {{ countDown }}秒后跳过
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      adImage: require('@/assets/ad.jpg'),
      countDown: 5,
      timer: null
    }
  },
  mounted() {
    this.startCountDown()
  },
  methods: {
    startCountDown() {
      this.timer = setInterval(() => {
        if (this.countDown <= 1) {
          this.$router.replace('/home')
        }
        this.countDown--
      }, 1000)
    },
    skipAd() {
      clearInterval(this.timer)
      this.$router.replace('/home')
    }
  }
}
</script>

<style scoped>
.splash-ad {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
}
.skip-btn {
  position: absolute;
  right: 20px;
  top: 20px;
  padding: 5px 10px;
  background: rgba(0,0,0,0.5);
  color: white;
  border-radius: 15px;
  cursor: pointer;
}
</style>

路由配置与广告显示控制

在路由配置中设置广告页为初始页面,并通过路由守卫控制显示逻辑:

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import SplashAd from './views/SplashAd.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'splash',
      component: SplashAd
    },
    {
      path: '/home',
      name: 'home',
      component: Home
    }
  ]
})

router.beforeEach((to, from, next) => {
  const hasSeenAd = localStorage.getItem('hasSeenAd')
  if (to.path === '/' && hasSeenAd) {
    next('/home')
  } else {
    next()
  }
})

export default router

广告显示频率控制

通过本地存储控制广告显示频率,避免每次打开都显示广告:

methods: {
  skipAd() {
    localStorage.setItem('hasSeenAd', 'true')
    clearInterval(this.timer)
    this.$router.replace('/home')
  }
}

广告数据动态获取

实际项目中广告内容通常从后端API获取:

async mounted() {
  try {
    const response = await fetch('https://api.example.com/ads')
    const data = await response.json()
    this.adImage = data.imageUrl
    this.startCountDown()
  } catch (error) {
    this.$router.replace('/home')
  }
}

性能优化建议

广告图片预加载可以提升用户体验:

created() {
  const img = new Image()
  img.src = this.adImage
  img.onload = () => {
    this.imageLoaded = true
  }
}

添加过渡效果使页面切换更平滑:

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

vue实现开屏广告

标签: 开屏广告
分享给朋友:

相关文章

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense…

uniapp广告联盟

uniapp广告联盟

Uniapp广告联盟接入指南 Uniapp作为跨平台开发框架,可通过广告联盟实现流量变现。主流广告平台均提供SDK或插件支持,需根据平台特性选择接入方式。 主流广告平台推荐 腾讯优量汇(原广点通)…

uniapp接入广告联盟

uniapp接入广告联盟

uniapp接入广告联盟的方法 选择广告联盟平台 uniapp支持接入多家广告联盟平台,包括腾讯优量汇、穿山甲、快手联盟等。根据应用类型和目标用户群体选择合适的平台,不同平台的广告收益和填充率有所差异…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

uniapp关闭广告

uniapp关闭广告

关闭广告的方法 在UniApp中关闭广告通常涉及以下几个方法,具体操作取决于广告的类型和集成方式。 移除广告模块 检查项目中是否集成了广告SDK或相关组件,如ad、ad-draw等。在pages.j…