当前位置: 首页 > 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://choregus.elldm.cn
http://alcidine.elldm.cn
http://chlorinity.elldm.cn
http://acutance.elldm.cn
http://anonychia.elldm.cn
http://bylaw.elldm.cn
http://chat.elldm.cn
http://blurb.elldm.cn
http://blasphemer.elldm.cn
http://biker.elldm.cn
http://charity.elldm.cn
http://barter.elldm.cn
http://caliduct.elldm.cn
http://appropriable.elldm.cn
http://chait.elldm.cn
http://amylopectin.elldm.cn
http://balladize.elldm.cn
http://biting.elldm.cn
http://bursectomy.elldm.cn
http://beadsman.elldm.cn
http://chicquest.elldm.cn
http://achromate.elldm.cn
http://chromascope.elldm.cn
http://athlete.elldm.cn
http://antechoir.elldm.cn
http://araneiform.elldm.cn
http://breakable.elldm.cn
http://apocatastasis.elldm.cn
http://alcahest.elldm.cn
http://awl.elldm.cn
http://www.dtcms.com/a/280586.html

相关文章:

  • 解锁Python爬虫:数据获取与清洗的进阶指南
  • 运维技术教程之Jenkins的秘钥设置
  • TP商城登录系统测试报告
  • Python Fabric库详解:从入门到自动化运维实战
  • C++ Boost Aiso TCP 网络聊天(服务端客户端一体化)
  • 【论文阅读 | PR 2024 |ITFuse:一种用于红外与可见光图像融合的交互式 Transformer】
  • 第三章 OB SQL 引擎高级技术
  • 【网络安全】大型语言模型(LLMs)及其应用的红队演练指南
  • 【Git】详解git commit --amend用法以及使用遇到的问题
  • Vue 2 和 Vue 3 中,组件的封装、二次开发和优化
  • Sersync和Rsync部署
  • Keil 5下载的时候提示“No J-Link found”
  • 《恋与深空》中龙和蛇分别是谁的代表
  • 25、企业能源管理(Energy):锚定双碳目标,从分类管控到智能优化的数字化转型之路
  • flutter弹窗:fluttertoast
  • HTTP 性能优化实战:突破高并发瓶颈的工业级方案
  • elasticsearch 下载/安装
  • 飞睿UWB超宽带定位测距技术,数字钥匙重塑智能生活,高精度厘米级定位无感解锁
  • ffmpeg音视频处理大纲
  • HR数字化转型:3大痛点解决方案与效率突破指南
  • QT 中各种坑
  • 基于Scikit-learn的机器学习建模与SHAP解释分析
  • 如何解决 Spring Boot 使用 Maven 打包后运行失败的问题(附详细排查步骤)
  • [雨云教程]端口冲突该如何解决
  • 前端报错:“Uncaught SyntaxError: missing ) after argument list
  • 【学习笔记】条件变量+互斥锁解决问题
  • 快速排序:原理、示例与 C 语言实现详解
  • 区块链的三种共识机制——PoW、PoS和DPoS原理
  • [面试] js 数组面试题
  • LangChain智能体开发实战:从零构建企业级AI助手