当前位置: 首页 > news >正文

uniapp 微信小程序Vue3项目使用内置组件movable-area封装悬浮可拖拽按钮(拖拽结束时自动吸附到最近的屏幕边缘)

一、最终效果

在这里插入图片描述

二、具体详情请看movable-area与movable-view官方文档说明

三、参数配置

1、代码示例

<TFab title="新建订单" @click="addOrder" />
// title:表按钮文案
// addOrder:点击按钮事件

四、组件源码

<template><movable-area class="movable-area" @touchend="onTouchend"><movable-view class="movable-view" :x="x" :y="y" direction="all" @change="onChange"><view class="addBtn" :style="{ width: `${width}px` , height: `${height}px`}" @tap="handleClick">{{title}}</view><slot /></movable-view></movable-area>
</template><script lang="ts" setup>
import { debounce } from "@/utils";
defineProps({title: {type: String},width: {type: Number,default: 40},height: {type: Number,default: 40}
});
const emits = defineEmits(["click"]);
const x = ref(0);
const y = ref(0);
const screenWidth = ref(0);
const screenHeight = ref(0);onMounted(() => {uni.getSystemInfo({success: res => {screenWidth.value = res.windowWidth;screenHeight.value = res.windowHeight;// 初始位置在屏幕右下角y.value = screenHeight.value - 200;x.value = screenWidth.value - 70;}});
});
// 拖动坐标更新(防抖)
const onChange = (e: { detail: { x: number; y: number } }) => {debounce(() => {x.value = e.detail.x;y.value = e.detail.y;}, 500);
};
// 触摸结束时吸附边缘
const onTouchend = () => {nextTick(() => {const threshold = 50; // 吸附阈值(rpx)if (Math.abs(x.value - 0) < threshold) {x.value = 0;} else if (Math.abs(x.value - screenWidth.value) < threshold) {x.value = screenWidth.value;}if (Math.abs(y.value - 0) < threshold) {y.value = 0;} else if (Math.abs(y.value - screenHeight.value) < threshold) {y.value = screenHeight.value;}});
};
const handleClick = () => {emits("click");
};
</script><style lang="scss">
.movable-area {position: fixed;top: 0;left: 0;width: 100vw;height: calc(100vh - 100px); // 可根据自己的项目来计算pointer-events: none; /* 关键样式 */z-index: 9999;.movable-view {pointer-events: auto; /* 关键样式 */width: 100rpx;height: 100rpx;will-change: transform;.addBtn {border-radius: 50%;width: 40px;height: 40px;overflow: hidden;display: flex;justify-content: center;align-items: center;color: #fff;font-size: 14px;padding: 8px;box-shadow: 0 1px 5px 2px rgba(0, 0, 0, 0.3);background: #355db4;text-align: center;}}
}
</style>

相关文章

基于ElementUi再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

http://www.lryc.cn/news/588715.html

相关文章:

  • Springboot儿童认知图文辅助系统6yhkv(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • LED 照明应用提供高性价比方案?会是你的首选吗?
  • Unity音游开发全指南:模板与免费资源高效构建节奏游戏
  • labview关于OOP
  • CG--类比推理
  • java截取视频帧
  • 视频HDR技术全解析:从原理到应用的深度探索
  • 音视频学习(三十九):IDR帧和I帧
  • React.FC与React.Component
  • PyCharm 高效入门指南:从安装到效率倍增
  • docker拉取nacos镜像失败
  • lanch4j将jar转成exe
  • 开通保存图片权限
  • iOS高级开发工程师面试——Swift
  • 语言模型玩转3D生成:LLaMA-Mesh开源项目
  • 无人机故障响应模块运行与技术难点
  • 全面安装指南:在Linux、Windows和macOS上部署Apache Cassandra
  • 网络劫持对用户隐私安全的影响:一场无形的数据窃取危机
  • 算法在前端框架中的集成
  • 021_自然语言处理应用
  • 量子比特的稳定性革命:破解量子计算“脆弱密码”的最新突破
  • 读取ubuntu的磁盘分区表与超级块
  • 【高等数学】第三章 微分中值定理与导数的应用——第一节 不定积分的概念与性质
  • 面向医疗AI场景的H20显卡算力组网方案
  • Vue 中 effectScope() 的全面解析与实战应用
  • WPF,Winform,HTML5网页,哪个UI开发速度最快?
  • 板凳-------Mysql cookbook学习 (十一--------11)
  • 使用 Java 获取 PDF 页面信息(页数、尺寸、旋转角度、方向、标签与边框)
  • PySpark Standalone 集群
  • PySpark 常用算子详解