看效果

这是一个全局的按钮,可以换成图片,自己写样式,每个页面都有;

须知:

1.uni.getSystemInfoSync()获取手机的信息接口

可以拿到手机屏幕的宽高

2.uni.createSelectorQuery().in(this)

uniapp中式没有window对象,和dom元素的,但是有时我们需要获取页面上节点的一些几何信息;

@touchcancel 手指触摸被打断,如来电提醒,弹窗
@touchend 手指触摸动作结束,如松开按钮
@touchmove 手指触摸后移动
@touchstart 手指触摸动作开始

3.touchmove滑动事件

@touchcancel 手指触摸被打断,如来电提醒,弹窗
@touchend 手指触摸动作结束,如松开按钮
@touchmove 手指触摸后移动
@touchstart 手指触摸动作开始

记录用户按下屏幕的坐标 x 和 y

touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				console.log('移动',e);
				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
				//将top存入本地存储
				 uni.setStorageSync("top", this.top);
			},
			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;
					}
				//将left存入本地存储
				 uni.setStorageSync("left", this.left);
				this.isMove = false;
				this.$emit('btnTouchend');
		}

每次移动这个按钮,本地存储的值都会改变;

取出存储的值

onShow() {
			//获取手机信息配置接口
			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;
			}
			//获取元素
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				console.log(data);
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				// this.left = this.windowWidth - this.width - this.edge;
				// this.top = this.windowHeight - this.height - this.edge;
				this.left = uni.getStorageSync('left')
				this.top=uni.getStorageSync('top')
				this.$nextTick(() => {
					this.firstIn = true
				})
			}).exec();
		},

赋值

<view id="_drag_button" class="drag" :style="{top:top 'px',left:left 'px',opacity:firstIn?1:0}"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@click.stop.prevent="click" :class="{transition: isDock && !isMove }">
			<button class="btn" open-type="contact" style="border: none;padding: 0;margin: 0;">
				<image class="img"
					src="图片地址">
				</image>
			</button>
		</view>

全局注册组件

因为我这个项目是vue3,所以注册组件的时候,不需要全局引入,

这个组件,需要在每个页面引入;
组件代码:需要换个图片就可以用了;

<template>
	<view>
		<view id="_drag_button" class="drag" :style="{top:top 'px',left:left 'px',opacity:firstIn?1:0}"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@click.stop.prevent="click" :class="{transition: isDock && !isMove }">
			<button class="btn" open-type="contact" style="border: none;padding: 0;margin: 0;">
				<image class="img"
					src="图片地址">
				</image>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
			}
		},
		data() {
			return {
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				text: ' ',
				firstIn: false
		onShow() {
			//获取手机信息配置接口
			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;
			//获取元素
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				console.log(data);
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				// this.left = this.windowWidth - this.width - this.edge;
				// this.top = this.windowHeight - this.height - this.edge;
				this.left = uni.getStorageSync('left')
				this.top=uni.getStorageSync('top')
				this.$nextTick(() => {
					this.firstIn = true
				})
			}).exec();
		methods: {
			click() {
				this.$emit('btnClick');
			touchstart(e) {
				this.$emit('btnTouchstart');
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				console.log('移动',e);
				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
				 uni.setStorageSync("top", this.top);
			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;
					}
				 uni.setStorageSync("left", this.left);
				this.isMove = false;
				this.$emit('btnTouchend');
		}
	}
</script>
<style lang="scss">
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 180rpx;
		height: 135rpx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;
		&.transition {
			transition: left .3s ease, top .3s ease;
	.btn {
		background-color: transparent;
		width: 135rpx;
		height: 130rpx;
		z-index: 9999;
	button::after {
		border: none;
	.img {
		height: 100%;
		width: 100%;
</style>

页面引入:

<drag-button :isDock="true" :existTabBar="true" @btnClick="btnClick" @btnTouchstart="btnTouchstart"
			@btnTouchend="btnTouchend">

引入组件

components: {
			dragButton
		},

到此这篇关于uniapp开发小程序如何实现全局悬浮按钮的文章就介绍到这了,更多相关uniapp小程序悬浮按钮内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

uniapp开发小程序实现全局悬浮按钮的代码的更多相关文章

  1. 微信小程序“圣诞帽”的实现思路详解

    这两天朋友圈被圣诞帽刷屏,下面通过本文给大家分享微信小程序“圣诞帽”的实现思路详解,需要的朋友参考下吧

  2. 小程序实现图片裁剪上传

    这篇文章主要为大家详细介绍了小程序实现图片裁剪上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. uniapp中vuex的应用使用步骤

    Vuex是一个专为Vue.js应用程序开发的状态管理模式,它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化,下面这篇文章主要给大家介绍了关于uniapp中vuex的应用使用,需要的朋友可以参考下

  4. php实现小程序支付完整版

    这篇文章主要为大家详细介绍了php实现小程序支付完整版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. 微信小程序的宿主环境实现代码

    这篇文章主要介绍了微信小程序的宿主环境,包括scroll-view 组件的基本使用,text 组件的基本使用及rich-text 组件的基本使用,本文通过示例代码给大家介绍的非常详细,需要的朋友可以参考下

  6. 微信小程序使用ucharts在小程序中加入横屏展示功能的全过程

    这篇文章主要给大家介绍了关于微信小程序使用ucharts在小程序中加入横屏展示功能的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用微信小程序具有一定的参考学习价值,需要的朋友可以参考下

  7. 微信小程序实现页面导航的方法详解

    这篇文章主要为大家详细介绍一下微信小程序实现页面导航的几种方法以及帮助大家掌握如何使用页面之间的导航跳转,感兴趣的可以了解一下

  8. 微信小程序开发WXML模板语法基础教程

    这篇文章主要介绍了微信小程序模板语法,WXML(WeiXin Markup Language)是框架设计的一套标签语言,结合基础组件、事件系统,可以构建出页面的结构,需要的朋友们下面随着小编来一起学习学习吧

  9. uniapp打包成微信小程序的详细过程

    微信小程序的出现给我们提供了一种使用应用的新方式和体验,下面这篇文章主要给大家介绍了关于uniapp打包成微信小程序的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下

  10. 微信小程序访问mysql数据库流程详解

    日常我们在开发小程序的时候,总是希望把数据提交回数据库进行存储,那在小程序中该如何访问数据库呢?本篇我们就介绍一下具体的思路

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部