uniapp悬浮按钮拖动效果

<template>
	<view>
		<movable-area class="movable-area">
			<movable-view class="movable-view" :x="left" :y="top" direction="all">
				<view
					id="_drag_action"
					class="drag"
					:style="'left: ' + left + 'px; top:' + top + 'px;'"
					@touchstart="touchstart"
					@touchmove="touchmove"
					@touchend="touchend"
					@click="click"
					:class="{transition: isDock && !isMove }"
				>
				</view>
			</movable-view>
		</movable-area>
	</view>
</template>

css代码

<style lang="scss">
	.movable-area {
			// top: 0;
			position: fixed;
			pointer-events: none;		//此处要加,鼠标事件可以渗透
			z-index: 100;
			.movable-view {
				pointer-events: auto;	//恢复鼠标事件
			}
		}
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		background: url('./icon/dragIcon.png');
		background-repeat: no-repeat;
		background-size: cover;
		color: $uni-text-color-inverse;
		width: 150upx;
		height: 150upx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;
		
		&.transition {
			transition: left .3s ease,top .3s ease;
		}
	}
	
</style>

js代码

<script>
	export default {
		name: 'card-drag',
		props: {
			isDock:{
				type: Boolean,
				default: false
			},
			existTabBar:{
				type: Boolean,
				default: false
			},
			location: {
				type: String, // topLeft, topRight, bottomLeft, bottomRight
				default: 'topRight'
			}
		},
		data() {
			return {
				top:0,
				left:0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			
			// #ifdef APP-PLUS
				this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)
			
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_action').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				switch(this.location) {
					case 'topLeft': 
					     this.left = this.width - this.edge; 
						 this.top = this.height - this.edge;
					break;
					case 'topRight': 
					      this.left = this.windowWidth - this.width - this.edge;
						  this.top = this.height - this.edge;
					break;
					case 'bottomLeft': 
					      this.left = this.width - this.edge;
						  this.top = this.windowHeight - this.height - this.edge;
					break;
					case 'bottomRight': 
					     this.left = this.windowWidth - this.width - this.edge;
					     this.top = this.windowHeight - this.height - this.edge;
					break;
				}
			}).exec();
		},
		methods: {
			click() {
				this.$emit('btnClick');
			},
			touchstart(e){
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				
				this.isMove = true;
				
				this.left = e.touches[0].clientX - this.offsetWidth;
				
				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
					clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
				}
				this.isMove = false;
				this.$emit('btnTouchend');
			},
		}}
</script>

效果如下:

 

悬浮按钮,可拖动,点击实现菜单功能,希望可以帮助你。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
uniapp悬浮按钮是一种按钮组件,可以让按钮悬浮在手机页面底部。它的实现方法可以通过在页面引入组件的方式来实现。在全局的页面app.vue中引入该组件,并将其写在状态栏的组件中,这样就可以在每个页面上引用该悬浮按钮。具体引入方法可以通过在页面代码中添加以下代码来实现: <drag-button :isDock="true" :existTabBar="true" @btnClick="btnClick" @btnTouchstart="btnTouchstart" @btnTouchend="btnTouchend"> 其中,:isDock="true"表示将按钮设置为悬浮在页面底部的状态,:existTabBar="true"表示按钮存在于底部导航栏上,@btnClick、@btnTouchstart和@btnTouchend则是按钮的点击、触摸开始和触摸结束事件。通过在app.vue中引入该组件后,在每个页面中都可以使用该悬浮按钮了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [令按钮悬浮在(手机)页面底部的实现方法](https://download.csdn.net/download/weixin_38713393/12781186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp开发小程序如何实现全局悬浮按钮](https://blog.csdn.net/qq_45018844/article/details/123630461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值