当前位置:首页 > 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 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现选人

vue实现选人

实现选人功能的基本思路 在Vue中实现选人功能通常涉及以下核心环节:数据绑定、用户交互处理、状态管理以及界面渲染。以下是具体实现方法: 数据准备与组件结构 创建包含人员信息的数组,通常从API获取或…

实现vue message

实现vue message

实现 Vue 消息提示组件 创建 Message 组件 在 src/components 目录下创建 Message.vue 文件,内容如下: <template> <tran…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…