当前位置:首页 > VUE

vue如何实现开屏广告

2026-02-21 07:01:09VUE

实现开屏广告的基本思路

在Vue中实现开屏广告通常需要结合路由守卫、定时器、本地存储等技术。核心逻辑是在应用启动时展示广告页面,倒计时结束后自动跳转到主页面,同时支持手动跳过。

使用路由守卫控制广告展示

通过Vue Router的全局前置守卫判断是否需要展示广告。可以利用localStoragesessionStorage记录广告是否已展示过:

// router.js
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: '/',
      redirect: '/splash'
    },
    {
      path: '/splash',
      name: 'splash',
      component: SplashAd
    },
    {
      path: '/home',
      name: 'home',
      component: Home
    }
  ]
})

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

广告页面组件实现

创建广告组件,包含倒计时逻辑和跳过按钮:

<template>
  <div class="splash-ad">
    <img src="@/assets/ad.jpg" alt="广告">
    <div class="skip-btn" @click="skipAd">
      跳过 {{countdown}}s
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      countdown: 5,
      timer: null
    }
  },
  mounted() {
    this.startCountdown()
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        if (this.countdown <= 1) {
          this.skipAd()
        } else {
          this.countdown--
        }
      }, 1000)
    },
    skipAd() {
      clearInterval(this.timer)
      localStorage.setItem('hasSeenAd', 'true')
      this.$router.replace('/home')
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
.splash-ad {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.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>

进阶优化方案

每日只展示一次广告

修改存储逻辑,记录最后展示日期:

// 在skipAd方法中
const today = new Date().toDateString()
localStorage.setItem('lastAdDate', today)

// 修改路由守卫
const lastAdDate = localStorage.getItem('lastAdDate')
const today = new Date().toDateString()
if (to.path === '/splash' && lastAdDate === today) {
  next('/home')
}

网络广告图片加载

使用异步加载广告图片,显示加载状态:

vue如何实现开屏广告

<template>
  <div v-if="loading" class="loading">加载中...</div>
  <div v-else class="splash-ad">
    <img :src="adImage" @load="onImageLoad">
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      adImage: ''
    }
  },
  mounted() {
    this.loadAdImage()
  },
  methods: {
    async loadAdImage() {
      try {
        const res = await fetch('https://api.example.com/ad')
        this.adImage = res.data.url
      } catch (e) {
        this.skipAd()
      }
    },
    onImageLoad() {
      this.loading = false
      this.startCountdown()
    }
  }
}
</script>

注意事项

  • 广告图片应预先压缩优化,减少加载时间
  • 移动端需考虑不同屏幕尺寸适配问题
  • 广告展示时间不宜过长,3-5秒为宜
  • 清除定时器防止内存泄漏
  • 提供无障碍访问支持

分享给朋友:

相关文章

vue如何实现

vue如何实现

Vue 实现方法 Vue 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pro…

java如何实现多线程

java如何实现多线程

实现多线程的方法 在Java中,实现多线程主要有两种方式:继承Thread类和实现Runnable接口。此外,还可以使用ExecutorService等高级工具类来管理线程。 继承Thread类 通…

如何实现vue验证

如何实现vue验证

Vue 表单验证的实现方法 Vue 表单验证可以通过多种方式实现,包括内置指令、第三方库和自定义验证逻辑。以下是几种常见的方法: 使用 Vue 内置指令进行基础验证 Vue 提供了 v-model…

react如何实现keepalive

react如何实现keepalive

React 实现 KeepAlive 的方法 React 本身没有内置的 KeepAlive 组件,但可以通过以下方式模拟类似功能,实现组件状态保持或避免重复渲染。 使用 CSS 隐藏组件 通过 C…

java如何实现分布式

java如何实现分布式

分布式系统实现方式 Java实现分布式系统通常涉及多种技术和框架,以下是几种核心方法: 基于RPC框架 使用Dubbo、gRPC等RPC框架实现服务间的远程调用。Dubbo提供高性能的RPC通信,支…

java如何实现单点登录

java如何实现单点登录

单点登录(SSO)的基本概念 单点登录是一种用户认证机制,允许用户通过一次登录访问多个相互信任的应用系统。核心原理是通过共享认证状态(如Token或Cookie)实现跨系统身份验证。 基于Token…