uniapp--悬浮可拖动按钮-实现思路

<template>
	<view class="content">
		<view :style="{'transform':'translate3d('+xMove+'px,'+yMove+'px,0)'}" class="touch" @touchstart="handleStart" @touchmove="handleMove" @touchend="handleEnd">↑</view>
	</view>
</template>

<script>
var curPoint = {
	x: 0,
	y: 0
}; // 记录原点
var startPoint = {};
// 标志位(只触发点击事件按,并没有移动-就不必触发end事件)
var isTouchMove = false;
export default {
	data() {
		return {
			xMove: 0,
			yMove: 0
		};
	},
	onLoad() {},
	mounted() {
		// 想通过过去节点来实现动态移动--这条路没有走通
		// let view = uni.createSelectorQuery().in(this);
		// view.select('.touch').boundingClientRect(data => {
		// 	console.log(data)
		// 	data.top = 100
		// }).exec();
		// let view = uni.createSelectorQuery().select('.touch');
		// view.fields({rect: true},data => {
		// 	console.log(data)
		// 	data.top = 100
		// }).exec();
	},
	methods: {
		handleStart(ev) {
			// console.log('start',ev);
			// 记录一开始手指按下的坐标
			var touch = ev.changedTouches[0];
			startPoint.x = touch.pageX;
			startPoint.y = touch.pageY;
		},
		handleMove(ev) {
			// console.log('move',ev);
			// 防止页面高度很大,出现滚动条,不能移动-默认拖动滚动条事件
			ev.preventDefault();

			isTouchMove = true;

			var touch = ev.changedTouches[0];
			var diffPonit = {}; // 存放差值
			var movePonit = {
				// 记录移动的距离
				x: 0,
				y: 0
			};
			diffPonit.x = touch.pageX - startPoint.x;
			diffPonit.y = touch.pageY - startPoint.y;
			// 移动的距离 = 差值 + 当前坐标点
			movePonit.x = diffPonit.x + curPoint.x;
			movePonit.y = diffPonit.y + curPoint.y;
			this.move(movePonit.x, movePonit.y);
		},
		handleEnd(ev) {
			// console.log('end', ev);
			if (!isTouchMove) return;

			//  更新坐标原点
			var touch = ev.changedTouches[0];

			curPoint.x += touch.pageX - startPoint.x;
			curPoint.y += touch.pageY - startPoint.y;

			// 重置
			isTouchMove = false;
		},
		move(x, y) {
			x = x || 0; // 没有传就是0
			y = y || 0;
			this.xMove = x;
			this.yMove = y;
			// translate3d	(tx,ty,tz)	在X轴偏移tx,在Y轴偏移ty,在Z轴偏移tz,单位px
		}
	}
};
</script>

<style>
.content {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
.touch {
	position: fixed;
	right: 20px;
	bottom: 20px;
	width: 45px;
	height: 45px;
	/* 知识点
		line-height是行高,针对的对象是文字,height针对的是容器,
		也就是高度,当height和line-height值相同时会居中,
		当line-height值小于height时文字向上移动,反之向下移动。
		 */
	line-height: 45px; /* 文字垂直居中 */
	text-align: center; /* 水平居中 */
	background-color: rgba(0, 0, 0, 0.6);
	border-radius: 50%;
	color: #fff;
	font-size: 30px;
	/* 去除标签点击事件高亮效果 */
	-webkit-tap-highlight-color: transparent;
	/* 使用transform: translate3d 处理性能高 GUP */
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,为您提供uni-app实现点击悬浮按钮展开悬浮菜单的步骤: 1. 在页面中添加一个悬浮按钮,如下: ```html <view class="float-btn" @tap="showMenu">+</view> ``` 2. 在data中添加一个变量,用来控制悬浮菜单的显示和隐藏,如下: ```javascript data() { return { isShowMenu: false, menuList: [ { text: '菜单1', icon: 'home', onClick() { // 处理菜单1的点击事件 } }, { text: '菜单2', icon: 'search', onClick() { // 处理菜单2的点击事件 } }, { text: '菜单3', icon: 'settings', onClick() { // 处理菜单3的点击事件 } } ] } } ``` 其中,menuList是悬浮菜单中的菜单列表,每个菜单都包括文本、图标和点击事件。 3. 在methods中添加一个方法,用来控制悬浮菜单的显示和隐藏,如下: ```javascript methods: { showMenu() { this.isShowMenu = !this.isShowMenu; }, handleMenuClick(index) { const menu = this.menuList[index]; if (menu.onClick) { menu.onClick(); } this.isShowMenu = false; } } ``` 其中,showMenu方法用于控制isShowMenu的值,从而显示或隐藏悬浮菜单;handleMenuClick方法用于处理悬浮菜单中的菜单被点击的事件。 4. 在template中添加悬浮菜单的代码,如下: ```html <view class="float-menu" v-show="isShowMenu"> <view class="menu-item" v-for="(menu, index) in menuList" :key="index" @tap="handleMenuClick(index)"> <view class="menu-icon"> <uni-icons :type="menu.icon"></uni-icons> </view> <view class="menu-text">{{ menu.text }}</view> </view> </view> ``` 其中,float-menu是悬浮菜单的容器,menu-item是悬浮菜单中的每个菜单项,menu-icon是菜单项的图标,menu-text是菜单项的文本。 5. 在CSS中设置悬浮按钮悬浮菜单的样式,如下: ```css .float-btn { position: fixed; right: 20px; bottom: 20px; width: 60px; height: 60px; line-height: 60px; text-align: center; font-size: 30px; border-radius: 50%; background-color: #007AFF; color: #fff; z-index: 9999; } .float-menu { position: fixed; right: 20px; bottom: 90px; width: 150px; height: 150px; border-radius: 10px; background-color: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3); z-index: 9998; display: flex; flex-wrap: wrap; justify-content: space-between; align-content: space-between; padding: 10px; opacity: 0.95; transition: all 0.3s; } .menu-item { width: 40%; height: 40%; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 12px; color: #333; } .menu-icon { font-size: 20px; color: #007AFF; } .menu-text { margin-top: 5px; } ``` 这样就可以实现用uni-app点击悬浮按钮展开悬浮菜单的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值