可拖拽悬浮按钮组件支持vue3/vue2

这个组件实现了一个可以在屏幕上拖拽移动的圆形悬浮按钮,具有以下特点:

  • 支持触摸移动功能
  • 可以切换显示图标(重载图标或加号图标)
  • 点击后触发扫描事件
  • 固定定位在屏幕上的浮动按钮样式

vue3版本

复制代码
<template>
  <view
    class="float-button"
    :style="{ bottom: buttonButtom + 'px', right: buttonRight + 'px' }"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    @touchstart="handleTouchStart"
    @click="goScan"
  >
    <uv-icon v-if="!show_add" name="scan" size="100rpx" color="#fff"></uv-icon>
    <uv-icon v-if="show_add" name="plus" size="70rpx" color="#fff"></uv-icon>
  </view>
</template>

<script setup>
import { ref } from "vue";
const buttonButtom = ref(180);
const buttonRight = ref(10);
const startX = ref(0);
const startY = ref(0);
const isDragging = ref(false);

// 扫码悬浮按钮封装
const props = defineProps({
  show_add: {
    type: Boolean,
    default: false, // 默认值
  },
});

const handleTouchStart = (event) => {
  startX.value = event.touches[0].pageX;
  startY.value = event.touches[0].pageY;
  isDragging.value = true;
};
const handleTouchMove = (event) => {
  if (isDragging.value) {
    const moveX = event.touches[0].pageX;
    const moveY = event.touches[0].pageY;
    buttonButtom.value += startY.value - moveY;
    buttonRight.value += startX.value - moveX;
    startX.value = moveX;
    startY.value = moveY;
  }
};
const handleTouchEnd = () => {
  isDragging.value = false;
};

const emit = defineEmits();

// 子组件传递给父组件的方法
const goScan = () => {
  emit("goScan");
};
</script>

<style>
.float-button {
  position: fixed;
  width: 120rpx;
  height: 120rpx;
  background-color: #003f98;
  color: white;
  text-align: center;
  line-height: 120rpx;
  border-radius: 50%;
  user-select: none;
  touch-action: none;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.5);
}
</style>

vue2版本

复制代码
<template>
  <view
    class="float-button"
    :style="{ bottom: buttonButtom + 'px', right: buttonRight + 'px' }"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    @touchstart="handleTouchStart"
    @click="goScan"
  >
    <uv-icon v-if="!show_add" name="reload" size="100rpx" color="#fff"></uv-icon>
    <uv-icon v-if="show_add" name="plus" size="70rpx" color="#fff"></uv-icon>
  </view>
</template>

<script>
export default {
  name: 'MoveBtn',
  props: {
    show_add: {
      type: Boolean,
      default: false // 默认值
    }
  },
  data() {
    return {
      buttonButtom: 180,
      buttonRight: 10,
      startX: 0,
      startY: 0,
      isDragging: false
    }
  },
  methods: {
    handleTouchStart(event) {
      this.startX = event.touches[0].pageX;
      this.startY = event.touches[0].pageY;
      this.isDragging = true;
    },
    handleTouchMove(event) {
      if (this.isDragging) {
        const moveX = event.touches[0].pageX;
        const moveY = event.touches[0].pageY;
        this.buttonButtom += this.startY - moveY;
        this.buttonRight += this.startX - moveX;
        this.startX = moveX;
        this.startY = moveY;
      }
    },
    handleTouchEnd() {
      this.isDragging = false;
    },
    goScan() {
      this.$emit("goScan");
    }
  }
}
</script>

<style>
.float-button {
  position: fixed;
  width: 120rpx;
  height: 120rpx;
  background-color: #003f98;
  color: white;
  text-align: center;
  line-height: 120rpx;
  border-radius: 50%;
  user-select: none;
  touch-action: none;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.5);
}
</style>
相关推荐
掘金安东尼4 小时前
让 JavaScript 更容易「善后」的新能力
前端·javascript·面试
掘金安东尼4 小时前
用 HTMX 为 React Data Grid 加速实时更新
前端·javascript·面试
灵感__idea6 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
yinuo6 小时前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队7 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher7 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati7 小时前
非常友好的Vue 3 生命周期详解
前端·面试
AAA阿giao7 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
亦妤7 小时前
JS执行机制、作用域及作用域链
javascript
兆子龙8 小时前
像 React Hook 一样「自动触发」:用 Git Hook 拦住忘删的测试代码与其它翻车现场
前端·架构