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

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

相关文章

uniapp关闭广告

uniapp关闭广告

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

vue实现弹出广告

vue实现弹出广告

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

vue实现漂浮广告

vue实现漂浮广告

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

css制作广告滚动

css制作广告滚动

使用CSS动画实现广告滚动 通过CSS的@keyframes和animation属性可以实现平滑的广告滚动效果。这种方法无需JavaScript,性能较好。 <div class="ad-co…

js实现广告拦截

js实现广告拦截

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

uniapp广告纠纷

uniapp广告纠纷

常见纠纷类型 涉及虚假宣传、点击欺诈、广告收益分成争议、流量作弊等问题。开发者可能因广告平台数据不透明或扣量行为产生质疑,广告主可能因投放效果未达预期而投诉。 解决方案 核对广告平台数据 要求广…