当前位置: 首页 > news >正文

uniapp实现全局悬浮框

uniapp实现全局悬浮框(按钮,页面,图片自行设置) 可拖动
话不多说直接上干货
1,在components新建组件(省去了每个页面都要引用组件的麻烦)
在这里插入图片描述
2,实现代码

<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 = thisthis.popupShow = getApp().globalData.callShowthis.top = getApp().globalData.callShowTop // 获取全局存储的位置,也可以使用本地缓存存储this.left = getApp().globalData.callShowLeftuni.$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.startTopthis.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.topgetApp().globalData.callShowLeft = this.leftuni.$emit(getApp().globalData.$global.CALL_SHOW_UPDATE) // 更新每个页面悬浮框位置},touchstart(e) {// console.log("------开始")// console.log(e)this.startTop = this.topthis.startLeft = this.leftthis.startClientTop = e.changedTouches[0].pageYthis.startClientLeft = e.changedTouches[0].pageX},callShowUpdate() {// 更新每个页面悬浮框位置this.top = getApp().globalData.callShowTopthis.left = getApp().globalData.callShowLeft},callShowOpen() {// 打开每个页面悬浮框this.popupShow = truegetApp().globalData.callShow = true},callShowClose() {// 关闭每个页面悬浮框this.popupShow = falsegetApp().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>
在 App.vue中全局存储悬浮框位置信息
globalData: {callShowTop: 100, // 悬浮框topcallShowLeft: 100, // 悬浮框leftcallShow: false, // 悬浮框是否显示
},

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

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

4,实现效果
请添加图片描述
每个页面切换后都会更新最新位置

http://www.lryc.cn/news/304669.html

相关文章:

  • C语言特殊函数
  • 全栈开发(TS,React,Vue, Java, 移动端flutter)接单
  • vue3使用百度地图
  • docker 安装达梦dm8 包含lincese
  • golang入门介绍-1
  • 273.【华为OD机试真题】园区参观路径(动态规划-JavaPythonC++JS实现)
  • ChatGPT调教指南 | 咒语指南 | Prompts提示词教程(二)
  • 超市售货|超市售货管理小程序|基于微信小程序的超市售货管理系统设计与实现(源码+数据库+文档)
  • 流程图:理解、创建与优化的视觉工具
  • Java EE面试题解析(下)
  • 从零开始手写mmo游戏从框架到爆炸(二十一)— 战斗系统二
  • 强化学习入门(Matlab2021b)-创建环境【3】
  • C++如何避免float误差?
  • 深信服技术认证“SCCA-C”划重点:深信服超融合HCI
  • Vue3路由元信息
  • 实用区块链应用:去中心化投票系统的部署与实施
  • Flink中的双流Join
  • 协程源码 launch 流程跟踪学习
  • 苍穹外卖Day02——总结2
  • 探索Nginx:一款高效、稳定的Web服务器和反向代理工具
  • 文案改写软件,高效改文案的方法
  • 黑马头条-day10
  • C++的stack容器->基本概念、常见接口
  • VUE中引入外部jquery.min.js文件
  • MongoDB聚合运算符:$avg
  • Web 前端 UI 框架Bootstrap简介与基本使用
  • 【Python笔记-设计模式】惰性评价模式
  • 每日学习总结20240221
  • 学生成绩管理系统(C语言课设 )
  • ChatGPT提示词(最新)