1.0.2 • Published 1 month ago

@tanzhenxing/zx-movable v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

zx-movable 可拖拽组件

基于uni-app官方movable-areamovable-view组件封装的高性能可拖拽组件。

特性

  • 🚀 高性能拖拽,基于原生组件实现
  • 🎯 支持多种拖拽方向(全方向、水平、垂直、禁用)
  • 🔄 支持双指缩放手势
  • 🎨 丰富的样式自定义选项
  • 📱 跨平台兼容(App、H5、各小程序平台)
  • 🎮 支持惯性滑动和边界回弹
  • 💫 流畅的动画效果
  • 🎛️ 灵活的API接口

平台兼容性

AppH5微信小程序支付宝小程序百度小程序抖音小程序QQ小程序快应用360小程序快手小程序京东小程序HarmonyOS

安装使用

<template>
  <view>
    <!-- 基础用法 -->
    <zx-movable />
    
    <!-- 自定义大小和内容 -->
    <zx-movable 
      :width="300" 
      :height="200"
      :view-width="80"
      :view-height="80"
      content="拖我试试"
    />
    
    <!-- 支持缩放 -->
    <zx-movable 
      :scale="true"
      :scale-min="0.5"
      :scale-max="3"
    />
    
    <!-- 水平方向拖拽 -->
    <zx-movable direction="horizontal" />
    
    <!-- 使用插槽自定义内容 -->
    <zx-movable>
      <view class="custom-content">
        <text>自定义内容</text>
      </view>
    </zx-movable>
  </view>
</template>

API

Props

参数类型默认值说明
widthString/Number200容器宽度(rpx)
heightString/Number200容器高度(rpx)
viewWidthString/Number50可拖拽元素宽度(rpx)
viewHeightString/Number50可拖拽元素高度(rpx)
contentString''默认显示文本
directionString'all'移动方向:all/vertical/horizontal/none
inertiaBooleanfalse是否带有惯性
outOfBoundsBooleanfalse超过边界后是否还可以移动
xString/Number0x轴方向偏移
yString/Number0y轴方向偏移
dampingNumber20阻尼系数,控制动画和回弹
frictionNumber2摩擦系数,控制惯性滑动
disabledBooleanfalse是否禁用
scaleBooleanfalse是否支持双指缩放
scaleMinNumber0.5最小缩放倍数
scaleMaxNumber10最大缩放倍数
scaleValueNumber1当前缩放倍数
animationBooleantrue是否使用动画
scaleAreaBooleanfalse缩放手势生效区域是否为整个movable-area
customClassString''自定义容器样式类
customStyleObject/String{}自定义容器样式
viewClassString''自定义可拖拽元素样式类
viewCustomStyleObject/String{}自定义可拖拽元素样式
backgroundColorString'#f5f5f5'容器背景颜色
viewBackgroundColorString'#007aff'拖拽元素背景颜色
textColorString'#ffffff'文字颜色
borderBooleantrue是否显示边框
borderRadiusString/Number8圆角大小(rpx)

Events

事件名说明参数
change拖拽过程中触发event对象,包含x、y、source
scale缩放过程中触发event对象
htouchmove水平拖拽时触发event对象
vtouchmove垂直拖拽时触发event对象
update:xx坐标更新时触发(支持v-model:x)x坐标值
update:yy坐标更新时触发(支持v-model:y)y坐标值

Methods

方法名说明参数
moveTo移动到指定位置(x, y, animated = true)
reset重置到初始位置-

Slots

插槽名说明
default自定义拖拽元素内容

使用示例

基础拖拽

<template>
  <zx-movable 
    :width="300"
    :height="200"
    content="拖拽我"
    @change="handleChange"
  />
</template>

<script setup>
const handleChange = (e) => {
  console.log('位置变化:', e.detail);
};
</script>

支持缩放

<template>
  <zx-movable 
    :scale="true"
    :scale-min="0.5"
    :scale-max="4"
    scale-area
    @scale="handleScale"
  />
</template>

<script setup>
const handleScale = (e) => {
  console.log('缩放倍数:', e.detail.scale);
};
</script>

双向绑定坐标

<template>
  <view>
    <zx-movable 
      v-model:x="posX"
      v-model:y="posY"
    />
    <text>当前位置: ({{ posX }}, {{ posY }})</text>
    <button @click="moveToCenter">移动到中心</button>
  </view>
</template>

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

const posX = ref(0);
const posY = ref(0);
const movableRef = ref();

const moveToCenter = () => {
  movableRef.value.moveTo(75, 75);
};
</script>

自定义样式

<template>
  <zx-movable 
    :width="300"
    :height="200"
    :view-width="80"
    :view-height="80"
    background-color="#f0f0f0"
    view-background-color="#ff4757"
    text-color="#ffffff"
    :border-radius="20"
  >
    <view class="custom-drag-item">
      <text class="drag-text">🚀</text>
    </view>
  </zx-movable>
</template>

<style>
.custom-drag-item {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.drag-text {
  font-size: 30rpx;
}
</style>

限制拖拽方向

<template>
  <view>
    <!-- 只能水平拖拽 -->
    <zx-movable 
      direction="horizontal"
      content="水平拖拽"
    />
    
    <!-- 只能垂直拖拽 -->
    <zx-movable 
      direction="vertical"
      content="垂直拖拽"
    />
    
    <!-- 禁用拖拽 -->
    <zx-movable 
      direction="none"
      content="禁用拖拽"
    />
  </view>
</template>

惯性滑动

<template>
  <zx-movable 
    :inertia="true"
    :friction="3"
    :damping="15"
    content="惯性滑动"
  />
</template>

注意事项

  1. movable-area 必须设置 widthheight 属性,不设置默认为 10px
  2. 在 app-nvue 平台暂不支持手势缩放,并且和滚动冲突
  3. 微信小程序基础库 2.4.4 起支持原生组件在可拖拽组件中的使用
  4. 缩放功能仅在支持双指手势的设备上有效
  5. 建议在使用时根据实际需求调整 dampingfriction 参数以获得最佳体验

更新日志

v1.0.0

  • 🎉 初始版本发布
  • ✨ 支持基础拖拽功能
  • ✨ 支持双指缩放
  • ✨ 支持多种拖拽方向
  • ✨ 支持样式自定义
  • ✨ 支持事件监听和方法调用