当前位置:首页 > VUE

vue实现浮窗游动

2026-01-21 15:59:58VUE

Vue实现浮窗游动效果

核心实现思路

使用Vue的响应式数据绑定和CSS动画结合,通过修改元素的transform属性实现游动效果。需要计算随机运动路径并平滑过渡。

vue实现浮窗游动

基础实现代码

<template>
  <div class="floating-window" :style="windowStyle">
    <!-- 浮窗内容 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      posX: 0,
      posY: 0,
      windowWidth: 200,
      windowHeight: 150
    }
  },
  computed: {
    windowStyle() {
      return {
        transform: `translate(${this.posX}px, ${this.posY}px)`,
        width: `${this.windowWidth}px`,
        height: `${this.height}px`
      }
    }
  },
  mounted() {
    this.startAnimation()
  },
  methods: {
    getRandomPosition() {
      const maxX = window.innerWidth - this.windowWidth
      const maxY = window.innerHeight - this.windowHeight
      return {
        x: Math.random() * maxX,
        y: Math.random() * maxY
      }
    },
    startAnimation() {
      const duration = 3000 + Math.random() * 4000
      const newPos = this.getRandomPosition()

      this.posX = newPos.x
      this.posY = newPos.y

      setTimeout(() => {
        this.startAnimation()
      }, duration)
    }
  }
}
</script>

<style>
.floating-window {
  position: fixed;
  background: white;
  box-shadow: 0 0 10px rgba(0,0,0,0.2);
  transition: transform 3s ease-in-out;
  z-index: 1000;
}
</style>

高级优化方案

增加贝塞尔曲线路径运动,使移动轨迹更自然。使用requestAnimationFrame实现平滑动画。

vue实现浮窗游动

methods: {
  animate() {
    const start = { x: this.posX, y: this.posY }
    const end = this.getRandomPosition()
    const control1 = this.getRandomControlPoint(start, end)
    const control2 = this.getRandomControlPoint(start, end)

    const startTime = performance.now()
    const duration = 3000

    const step = (currentTime) => {
      const elapsed = currentTime - startTime
      const progress = Math.min(elapsed / duration, 1)

      // 三次贝塞尔曲线计算
      this.posX = this.cubicBezier(
        start.x, control1.x, control2.x, end.x, progress
      )
      this.posY = this.cubicBezier(
        start.y, control1.y, control2.y, end.y, progress
      )

      if (progress < 1) {
        requestAnimationFrame(step)
      } else {
        this.animate()
      }
    }

    requestAnimationFrame(step)
  },
  cubicBezier(p0, p1, p2, p3, t) {
    const mt = 1 - t
    return mt*mt*mt*p0 + 3*mt*mt*t*p1 + 3*mt*t*t*p2 + t*t*t*p3
  }
}

边界检测处理

防止浮窗移出可视区域,在随机位置生成时增加边界检查。

getRandomPosition() {
  const padding = 20
  const maxX = window.innerWidth - this.windowWidth - padding
  const maxY = window.innerHeight - this.windowHeight - padding
  return {
    x: padding + Math.random() * maxX,
    y: padding + Math.random() * maxY
  }
}

性能优化建议

  1. 使用will-change: transform提升动画性能
  2. 适当降低动画频率,避免过度消耗资源
  3. 在组件销毁时清除动画循环
beforeDestroy() {
  cancelAnimationFrame(this.animationId)
}

标签: vue
分享给朋友:

相关文章

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…

vue实现选人

vue实现选人

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