当前位置:首页 > VUE

vue实现双摇杆

2026-02-17 01:07:13VUE

双摇杆实现思路

双摇杆通常用于游戏或交互式应用中,一个摇杆控制角色移动,另一个控制视角或攻击方向。Vue中可以通过监听触摸或鼠标事件实现。

基础HTML结构

创建两个摇杆的DOM元素,分别用不同类名标识:

<template>
  <div class="joystick-container">
    <div class="joystick-movement" ref="movementJoystick"></div>
    <div class="joystick-aiming" ref="aimingJoystick"></div>
  </div>
</template>

样式设置

使用CSS定位摇杆元素,设置基本样式:

.joystick-container {
  position: relative;
  width: 100%;
  height: 100vh;
}

.joystick-movement, .joystick-aiming {
  position: absolute;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.3);
}

.joystick-movement {
  bottom: 50px;
  left: 50px;
}

.joystick-aiming {
  bottom: 50px;
  right: 50px;
}

摇杆逻辑实现

在Vue组件中实现摇杆控制逻辑:

vue实现双摇杆

export default {
  mounted() {
    this.initJoystick(this.$refs.movementJoystick, 'movement');
    this.initJoystick(this.$refs.aimingJoystick, 'aiming');
  },
  methods: {
    initJoystick(element, type) {
      const stick = document.createElement('div');
      stick.className = 'joystick-handle';
      element.appendChild(stick);

      const rect = element.getBoundingClientRect();
      const center = {
        x: rect.left + rect.width / 2,
        y: rect.top + rect.height / 2
      };

      const handleMove = (e) => {
        const clientX = e.touches ? e.touches[0].clientX : e.clientX;
        const clientY = e.touches ? e.touches[0].clientY : e.clientY;

        const deltaX = clientX - center.x;
        const deltaY = clientY - center.y;
        const distance = Math.min(Math.sqrt(deltaX * deltaX + deltaY * deltaY), rect.width / 2);

        const angle = Math.atan2(deltaY, deltaX);
        const x = distance * Math.cos(angle);
        const y = distance * Math.sin(angle);

        stick.style.transform = `translate(${x}px, ${y}px)`;

        // 根据type分发不同事件
        const event = new CustomEvent(`${type}-joystick`, {
          detail: {
            x: x / (rect.width / 2),
            y: y / (rect.height / 2)
          }
        });
        window.dispatchEvent(event);
      };

      const handleEnd = () => {
        stick.style.transform = 'translate(0, 0)';
        window.dispatchEvent(new CustomEvent(`${type}-joystick`, {
          detail: { x: 0, y: 0 }
        }));
      };

      element.addEventListener('touchstart', (e) => {
        e.preventDefault();
        handleMove(e);
        document.addEventListener('touchmove', handleMove);
        document.addEventListener('touchend', handleEnd, { once: true });
      });

      element.addEventListener('mousedown', () => {
        document.addEventListener('mousemove', handleMove);
        document.addEventListener('mouseup', handleEnd, { once: true });
      });
    }
  }
}

摇杆手柄样式

添加摇杆手柄的CSS样式:

.joystick-handle {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

事件监听使用

在其他组件中监听摇杆事件:

vue实现双摇杆

mounted() {
  window.addEventListener('movement-joystick', (e) => {
    console.log('移动摇杆:', e.detail);
  });

  window.addEventListener('aiming-joystick', (e) => {
    console.log('瞄准摇杆:', e.detail);
  });
}

摇杆限制优化

添加摇杆移动范围限制,防止手柄超出摇杆区域:

const distance = Math.min(
  Math.sqrt(deltaX * deltaX + deltaY * deltaY), 
  rect.width / 2
);

移动平滑处理

添加缓动效果使摇杆移动更平滑:

stick.style.transition = distance === 0 ? 'transform 0.2s ease-out' : 'none';

销毁事件监听

在组件销毁时移除事件监听:

beforeDestroy() {
  window.removeEventListener('movement-joystick');
  window.removeEventListener('aiming-joystick');
}

标签: 摇杆vue
分享给朋友:

相关文章

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-toa…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现选择框

vue实现选择框

Vue 实现选择框的方法 Vue 中可以通过多种方式实现选择框(下拉框),包括原生 HTML 的 <select> 元素结合 Vue 的数据绑定,或者使用第三方 UI 库如 Element…

vue路由实现滑动

vue路由实现滑动

实现 Vue 路由滑动效果的方法 使用 Vue Router 结合 CSS 过渡动画 在 Vue Router 的路由视图组件 <router-view> 上添加过渡效果,结合 CSS 实…