当前位置:首页 > 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 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓、面…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templ…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue请求实现进度条

vue请求实现进度条

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