实现悬浮按钮拖动,兼容h5和微信小程序

embedded/2025/3/16 20:08:44/

h5用js写,小程序>微信小程序

代码里面没有完全实现吸附边缘的功能,需要吸附边缘的话还得自己再完善下(h5的吸附边缘是可以的,小程序的还有点问题)

主要功能是:图片上写文字的悬浮按钮,文字使用的是::after实现的,图片就用的u-image标签

(图片和文字,用背景图好像更方便诶,文字就用绝对定位、flex或者是margin)

		<template><!-- #ifndef MP-WEIXIN --><view class="btn" :style="{ left: left + 'px', top: top + 'px' }" @touchstart="handleTouchStart"@touchmove.prevent="handleTouchMove" @touchend="handleTouchEnd" @click="clickHandle"><u--image src="放自己的图" width="144rpx"height="128rpx" class="link"></u--image></view><!-- #endif --><!-- #ifdef MP-WEIXIN --><movable-area class="drag-area" style="height:100vh"><movable-view direction="all" :x="x" :y="y" :damping="40" @touchend="handleTouchEnd" class="btn"@click="clickHandle" :animation="false"><u--image src="放自己的图"width="144rpx" height="128rpx"></u--image></movable-view></movable-area><!-- #endif --></template>left: 0,     // 按钮左侧位置top: 0,      // 按钮顶部位置startX: 0,   // 触摸起始X坐标startY: 0,screenWidth: 0,  // 屏幕宽度screenHeight: 0, // 屏幕高度x: 0,           // X轴位置y: 500,           // Y轴位置systemInfo: {},  // 系统信息btnWidth: 72,    // 按钮宽度(px)btnHeight: 64,   // 按钮高度(px)mounted() {const systemInfo = uni.getSystemInfoSync();this.screenWidth = systemInfo.windowWidth;this.screenHeight = systemInfo.windowHeight;this.left = this.screenWidth - this.btnWidth - 10;this.top = 500;this.x = this.screenWidth - this.btnWidth - 10;this.y = 500;},handleTouchStart(e) {// 记录触摸起点this.startX = e.touches[0].clientX;this.startY = e.touches[0].clientY;},handleTouchMove(e) {if (e.cancelable) e.preventDefault();const deltaX = e.touches[0].clientX - this.startX;const deltaY = e.touches[0].clientY - this.startY;let newLeft = this.left + deltaX;let newTop = this.top + deltaY;newLeft = Math.max(0, Math.min(newLeft, this.screenWidth - this.btnWidth));newTop = Math.max(0, Math.min(newTop, this.screenHeight - this.btnHeight));this.left = newLeft;this.top = newTop;this.startX = e.touches[0].clientX;this.startY = e.touches[0].clientY;},handleTouchEnd() {// this.autoAttachToEdge();},// 拖动过程中触发onDragChange(e) {// 实时更新位置// this.x = e.detail.x// this.y = e.detail.y},autoAttachToEdge() {// 吸附到左侧或右侧const midScreen = this.screenWidth / 2;if (this.left < midScreen) {this.left = 10; // 吸附到左边} else {this.left = this.screenWidth - this.btnWidth - 10; // 吸附到右边}},// 点击按钮的逻辑clickHandle(){}.btn {position: fixed;z-index: 999;}/* #ifndef MP-WEIXIN */.link {&::after {content: 'XXX';font-size: 24px;color: #fff;position: absolute;bottom: 4px;left: 0;line-height: 34px;width: 100%;text-align: center;z-index: 999;}}/* #endif *//* #ifdef MP-WEIXIN */.btn ::v-deep .u-image::after {content: 'XXX';font-size: 24px;color: #fff;position: absolute;bottom: 4px;left: 0;line-height: 34px;width: 100%;text-align: center;z-index: 999;}/* #endif *//* 拖拽区域必须设置尺寸 */.drag-area {width: 100%;position: fixed;pointer-events: none;/* 防止遮挡下方内容 */z-index: 999;}/* 可拖拽按钮样式 */.btn {width: 144rpx;height: 128rpx;pointer-events: auto;/* 允许交互 */}/* 隐藏movable-view默认边框 */movable-view::before {display: none;}


http://www.ppmy.cn/embedded/173148.html

相关文章

Java程序开发之Spring Security实战:JWT实现登录鉴权

一、JWT与安全认证核心原理 1. JWT结构解析 Header(头部) { "alg": "HS256", "typ": "JWT" } Payload(负载) { "sub": "user123", "exp": 1680403200, "roles": ["US…

嵌入式硬件: GPIO与二极管基础知识详解

1. 前言 在嵌入式系统和硬件开发中&#xff0c;GPIO&#xff08;通用输入输出&#xff09;是至关重要的控制方式&#xff0c;而二极管作为基础电子元件&#xff0c;广泛应用于信号整流、保护电路等。本文将从基础原理出发&#xff0c;深入解析GPIO的输入输出模式&#xff0c;包…

PyTorch 深度学习实战(14):Deep Deterministic Policy Gradient (DDPG) 算法

在上一篇文章中&#xff0c;我们介绍了 Proximal Policy Optimization (PPO) 算法&#xff0c;并使用它解决了 CartPole 问题。本文将深入探讨 Deep Deterministic Policy Gradient (DDPG) 算法&#xff0c;这是一种用于连续动作空间的强化学习算法。我们将使用 PyTorch 实现 D…

【C++】STL全面简介与string类的使用(万字解析)

文章目录 一、STL简介1. 什么是STL2. STL的版本3. STL的组成4. 如何学习STL 二、string类的使用第一部分第二部分第三部分第四部分(含auto与范围for)第五部分第六部分第七部分 一、STL简介 1. 什么是STL STL(standard template libaray-标准模板库)&#xff1a;是C标准库的重要…

[JAVASE] Collection集合的遍历

一. 集合分类 java中的Collection集合分为两类, 分别是单列集合(List)和双列(Map)集合. 1.1 单列集合 1.2 双列集合 二. 集合遍历 2.1 List单列集合的遍历 for each遍历 迭代器遍历 lambda遍历 2.2 Map双列集合的遍历 for each遍历 k-v整体遍历 lambda表达式遍历

Qt 实现波浪填充的圆形进度显示

话不多说&#xff0c;先上效果图 代码示例&#xff1a; #include <QApplication> #include <QWidget> #include <QPainter> #include <QPropertyAnimation> #include <QTimer> #include <cmath>class WaveProgressBar : public QWidget {…

[设计模式]1_设计模式概览

摘要&#xff1a;设计模式原则、设计模式的划分与简要概括&#xff0c;怎么使用重构获得设计模式并改善代码的坏味道。 本篇作概览与检索用&#xff0c;后续结合源码进行具体模式深入学习。 目录 1、设计模式原理 核心原则&#xff08;语言无关&#xff09; 本质原理图 原…

珠算与珠心算发展简介

珠算是中华传统优秀文化的科学遗产&#xff0c;它是我国劳动人民的伟大创造&#xff0c;被誉为中国的第五大发明&#xff0c;至今已有 1800 余年的历史。 珠算&#xff0c;是以算盘为工具&#xff0c;用手指拨动算珠进行数值计算的一门计算技术。同时&#xff0c;珠算又是一门科…