当前位置:首页 > VUE

vue实现漂浮广告

2026-01-19 04:27:10VUE

实现漂浮广告的基本思路

在Vue中实现漂浮广告可以通过动态样式绑定和定时器控制广告元素的位置。核心是利用CSS的position: fixedabsolute定位,结合JavaScript修改top/left等属性实现移动效果。

基础实现代码示例

<template>
  <div 
    class="floating-ad"
    :style="{
      left: position.x + 'px',
      top: position.y + 'px',
    }"
    @mouseenter="pauseMove"
    @mouseleave="resumeMove"
  >
    <!-- 广告内容 -->
    <img src="ad-image.png" alt="广告">
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: { x: 0, y: 0 },
      direction: { x: 1, y: 1 },
      speed: 2,
      moveInterval: null
    }
  },
  mounted() {
    this.startMove();
  },
  beforeDestroy() {
    clearInterval(this.moveInterval);
  },
  methods: {
    startMove() {
      this.moveInterval = setInterval(() => {
        this.position.x += this.direction.x * this.speed;
        this.position.y += this.direction.y * this.speed;

        // 边界检测
        if (this.position.x <= 0 || this.position.x >= window.innerWidth - 100) {
          this.direction.x *= -1;
        }
        if (this.position.y <= 0 || this.position.y >= window.innerHeight - 100) {
          this.direction.y *= -1;
        }
      }, 16);
    },
    pauseMove() {
      clearInterval(this.moveInterval);
    },
    resumeMove() {
      this.startMove();
    }
  }
}
</script>

<style>
.floating-ad {
  position: fixed;
  width: 100px;
  height: 100px;
  cursor: pointer;
  z-index: 9999;
}
</style>

优化方向

边界限制增强 通过window.innerWidthwindow.innerHeight动态获取可视区域尺寸,确保广告不会超出屏幕范围。

vue实现漂浮广告

性能优化 使用requestAnimationFrame替代setInterval可获得更流畅的动画效果:

move() {
  this.position.x += this.direction.x * this.speed;
  this.position.y += this.direction.y * this.speed;

  // 边界检测逻辑
  if (this.position.x <= 0 || this.position.x >= window.innerWidth - 100) {
    this.direction.x *= -1;
  }
  if (this.position.y <= 0 || this.position.y >= window.innerHeight - 100) {
    this.direction.y *= -1;
  }

  this.animationId = requestAnimationFrame(this.move);
}

关闭功能 添加关闭按钮提升用户体验:

vue实现漂浮广告

<div class="floating-ad">
  <span class="close-btn" @click="closeAd">×</span>
  <!-- 广告内容 -->
</div>

<style>
.close-btn {
  position: absolute;
  right: 5px;
  top: 0;
  cursor: pointer;
}
</style>

移动端适配

针对移动设备需要添加触摸事件支持:

@touchstart="pauseMove"
@touchend="resumeMove"

并调整广告尺寸:

@media (max-width: 768px) {
  .floating-ad {
    width: 80px;
    height: 80px;
  }
}

标签: 广告vue
分享给朋友:

相关文章

vue 实现目录

vue 实现目录

Vue 实现目录的方法 在 Vue 项目中实现目录功能,可以通过以下几种方式实现,具体取决于需求和技术栈。 使用 Vue Router 实现路由目录 Vue Router 是 Vue.js 的官方路…

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…