uniapp实现全局悬浮框

uniapp实现全局悬浮框(按钮,页面,图片自行设置) 可拖动

话不多说直接上干货

1,在components新建组件(省去了每个页面都要引用组件的麻烦)

2,实现代码

c 复制代码
<template>
	<view class="call-plate" :style="'top:' + top + 'px;left:' + left + 'px;'" @touchmove="touchmove" @touchend="touchend" @touchstart="touchstart" v-if="popupShow">
		通话中悬浮框
	</view>
</template>

<script>
	export default {
		name: "call-screen",
		emits: ["hide", "confirm"],
		props: {
			/**
			 * 默认号码
			 */
			number: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				popupShow: true, // 是否显示当前页面
				top: 0,
				left: 0,
				startTop: 0,
				startLeft: 0,
				startClientTop: 0,
				startClientLeft: 0,
			}
		},
		watch: {
			
		},
		computed: {
			i18n() {
				return this.$t
			}
		},
		created() {
			let that = this
			this.popupShow = getApp().globalData.callShow
			this.top = getApp().globalData.callShowTop // 获取全局存储的位置,也可以使用本地缓存存储
			this.left = getApp().globalData.callShowLeft
			uni.$on(getApp().globalData.$global.CALL_SHOW_UPDATE, this.callShowUpdate)
			uni.$on(getApp().globalData.$global.CALL_SHOW_OPEN, this.callShowOpen)
			uni.$on(getApp().globalData.$global.CALL_SHOW_CLOSE, this.callShowClose)
		},
		destroyed() {
		    // 销毁通知
			uni.$off(getApp().globalData.$global.CALL_SHOW_UPDATE, this.callShowUpdate)
			uni.$off(getApp().globalData.$global.CALL_SHOW_OPEN, this.callShowOpen)
			uni.$off(getApp().globalData.$global.CALL_SHOW_CLOSE, this.callShowClose)
		},
		methods: {
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				// console.log(e)
				this.top = e.changedTouches[0].pageY - this.startClientTop + this.startTop
				this.left = e.changedTouches[0].pageX - this.startClientLeft + this.startLeft
			},
			touchend(e) {
				// console.log("------结束,top:" + this.top + ",left:" + this.left)
				// console.log(e)
				getApp().globalData.callShowTop = this.top
				getApp().globalData.callShowLeft = this.left
				uni.$emit(getApp().globalData.$global.CALL_SHOW_UPDATE) // 更新每个页面悬浮框位置
			},
			touchstart(e) {
				// console.log("------开始")
				// console.log(e)
				this.startTop = this.top
				this.startLeft = this.left
				this.startClientTop = e.changedTouches[0].pageY
				this.startClientLeft = e.changedTouches[0].pageX
			},
			callShowUpdate() {
				// 更新每个页面悬浮框位置
				this.top = getApp().globalData.callShowTop
				this.left = getApp().globalData.callShowLeft
			},
			callShowOpen() {
				// 打开每个页面悬浮框
				this.popupShow = true
				getApp().globalData.callShow = true
			},
			callShowClose() {
				// 关闭每个页面悬浮框
				this.popupShow = false
				getApp().globalData.callShow = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	.call-plate {
		display: flex;
		position: absolute;
		width: 90px;
		height: 160px;
		z-index: 9999999;
		background-color: yellow;
	}
</style>
c 复制代码
在 App.vue中全局存储悬浮框位置信息
globalData: {
	callShowTop: 100, // 悬浮框top
	callShowLeft: 100, // 悬浮框left
	callShow: false, // 悬浮框是否显示
},

3,在每个需要用到悬浮框的页面引入

c 复制代码
<template>
	<view class="content">
        <!--组件引用-->
		<call-screen></call-screen>
	</view>
</template>
c 复制代码
发通知控制显示隐藏悬浮框
uni.$emit(that.global.CALL_SHOW_CLOSE)
uni.$emit(that.global.CALL_SHOW_OPEN)

4,实现效果

每个页面切换后都会更新最新位置

相关推荐
LaughingZhu9 分钟前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫15 分钟前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux1 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水2 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger2 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)2 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态2 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态2 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart3 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe53 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架