Skip to content

MoveIcon 悬浮按钮

可拖动的悬浮按钮组件,支持自动停靠到屏幕边缘,常用于快捷操作入口。

基础用法

vue
<template>
  <dr-move-icon @click="handleClick">
    <view class="custom-btn">
      <text>+</text>
    </view>
  </dr-move-icon>
</template>

<script setup>
const handleClick = () => {
  console.log('点击了悬浮按钮')
}
</script>

<style>
.custom-btn {
  width: 100%;
  height: 100%;
  background: #409eff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 48rpx;
}
</style>

自动停靠

默认开启自动停靠功能,拖动结束后会自动吸附到最近的屏幕边缘。

vue
<template>
  <!-- 开启自动停靠(默认) -->
  <dr-move-icon docking />

  <!-- 关闭自动停靠 -->
  <dr-move-icon :docking="false" />
</template>

停靠方向

可以设置只停靠到左边、右边或两边都可以停靠。

vue
<template>
  <!-- 两边都可停靠(默认) -->
  <dr-move-icon docking-side="both" />

  <!-- 只停靠左边 -->
  <dr-move-icon docking-side="left" />

  <!-- 只停靠右边 -->
  <dr-move-icon docking-side="right" />
</template>

初始位置

通过 bottomright 属性设置悬浮按钮的初始位置。

vue
<template>
  <!-- 距离底部 100px,距离右边 20px -->
  <dr-move-icon :bottom="100" :right="20" />
</template>

禁用拖动

vue
<template>
  <dr-move-icon disabled @click="handleClick">
    <view class="btn">固定按钮</view>
  </dr-move-icon>
</template>

自定义尺寸

通过 widthheight 属性设置按钮尺寸(单位:rpx)。

vue
<template>
  <!-- 大尺寸按钮 -->
  <dr-move-icon :width="120" :height="120">
    <view class="large-btn">大</view>
  </dr-move-icon>

  <!-- 小尺寸按钮 -->
  <dr-move-icon :width="80" :height="80">
    <view class="small-btn">小</view>
  </dr-move-icon>
</template>

拖动事件

vue
<template>
  <dr-move-icon
    @click="handleClick"
    @dragstart="handleDragStart"
    @dragend="handleDragEnd"
    @change="handleChange"
  >
    <view class="btn">拖我</view>
  </dr-move-icon>
</template>

<script setup>
const handleClick = () => {
  console.log('点击事件')
}

const handleDragStart = (e) => {
  console.log('开始拖动', e.x, e.y)
}

const handleDragEnd = (e) => {
  console.log('结束拖动', e.x, e.y)
}

const handleChange = (e) => {
  console.log('位置变化', e.x, e.y, e.source)
}
</script>

Ref 方法

通过 ref 可以调用组件方法。

vue
<template>
  <dr-move-icon ref="moveIconRef">
    <view class="btn">按钮</view>
  </dr-move-icon>
  <button @click="reset">重置位置</button>
  <button @click="setPos">设置位置</button>
</template>

<script setup>
import { ref } from 'vue'

const moveIconRef = ref(null)

const reset = () => {
  // 重置到初始位置
  moveIconRef.value.resetPosition()
}

const setPos = () => {
  // 设置到指定位置
  moveIconRef.value.setPosition(100, 200)
}
</script>

API

Props

参数说明类型默认值
disabled是否禁用拖动booleanfalse
docking是否自动停靠到边缘booleantrue
docking-side停靠方向'both' | 'left' | 'right''both'
bottom初始位置距离底部距离(px)number100
right初始位置/停靠右边时的边距(px)number10
left停靠左边时的边距(px)number10
width按钮宽度(rpx)number100
height按钮高度(rpx)number100
inertia是否开启惯性booleantrue
damping阻尼系数,越大移动越快number20
friction摩擦系数,越大滑动越快停止number2
out-of-bounds是否允许超出边界booleanfalse
z-index层级number5000
animation-duration停靠动画时长(ms)number300

Events

事件名说明回调参数
click点击按钮时触发-
dragstart开始拖动时触发{ x: number, y: number }
dragend结束拖动时触发{ x: number, y: number }
change位置变化时触发{ x: number, y: number, source: string }

Methods

通过 ref 可以调用以下方法:

方法名说明参数
resetPosition重置到初始位置-
setPosition设置到指定位置(x: number, y: number)

Slots

名称说明
default自定义按钮内容

注意事项

  1. 组件使用 movable-areamovable-view 实现,兼容 H5、微信小程序、App 等多端
  2. 按钮内容需要通过插槽传入,组件提供默认的圆形按钮样式
  3. bottomright 属性使用 px 单位,widthheight 使用 rpx 单位
  4. 停靠动画使用 CSS transition 实现,在部分低版本设备上可能不够流畅
  5. 如果页面有多个悬浮按钮,注意调整 z-index 避免层级冲突

基于 MIT 许可发布