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

路由配置与广告显示控制

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

vue实现开屏广告

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获取:

vue实现开屏广告

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;
}

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

相关文章

js实现开屏动画

js实现开屏动画

使用CSS和JavaScript实现开屏动画 通过CSS定义动画效果,JavaScript控制动画的触发和结束时机。 <!DOCTYPE html> <html> <h…

js广告弹窗实现

js广告弹窗实现

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

uniapp关闭广告

uniapp关闭广告

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

uniapp广告弹窗

uniapp广告弹窗

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

vue实现广告组件

vue实现广告组件

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

css制作广告

css制作广告

CSS制作广告的基本方法 使用CSS可以轻松创建吸引人的广告效果,以下是几种常见的方法: 创建基础广告容器 通过div元素创建广告容器,设置固定宽度、高度和背景颜色: .ad-container…