当前位置:首页 > VUE

vue实现浮窗游动

2026-01-21 15:59:58VUE

Vue实现浮窗游动效果

核心实现思路

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

基础实现代码

<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实现平滑动画。

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 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contain…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…