uniapp开发小程序实现全局悬浮按钮的代码_第1页
uniapp开发小程序实现全局悬浮按钮的代码_第2页
uniapp开发小程序实现全局悬浮按钮的代码_第3页
uniapp开发小程序实现全局悬浮按钮的代码_第4页
uniapp开发小程序实现全局悬浮按钮的代码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第uniapp开发小程序实现全局悬浮按钮的代码目录看效果须知:1.uni.getSystemInfoSync()获取手机的信息接口2.uni.createSelectorQuery().in(this)3.touchmove滑动事件取出存储的值赋值全局注册组件

看效果

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

须知:

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){

returnfalse;

console.log('移动',e);

this.isMove=true;

this.left=e.touches[0].clientX-this.offsetWidth;

letclientY=e.touches[0].clientY-this.offsetHeight;

//#ifdefH5

clientY+=this.height;

//#endif

letedgeBottom=this.windowHeight-this.height-this.edge;

//上下触及边界

if(clientYthis.edge){

this.top=this.edge;

}elseif(clientYedgeBottom){

this.top=edgeBottom;

}else{

this.top=clientY

//将top存入本地存储

uni.setStorageSync("top",this.top);

touchend(e){

if(this.isDock){

letedgeRigth=this.windowWidth-this.width-this.edge;

if(this.leftthis.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(){

//获取手机信息配置接口

constsys=uni.getSystemInfoSync();

//屏幕的宽高

this.windowWidth=sys.windowWidth;

this.windowHeight=sys.windowHeight;

//#ifdefAPP-PLUS

this.existTabBar(this.windowHeight-=50);

//#endif

if(sys.windowTop){

this.windowHeight+=sys.windowTop;

//获取元素

constquery=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();

赋值

viewid="_drag_button":style="{top:top+'px',left:left+'px',opacity:firstIn1:0}"

@touchstart="touchstart"@touchmove.stop.prevent="touchmove"@touchend="touchend"

@click.stop.prevent="click":

buttonopen-type="contact"

imagesrc="图片地址"

/image

/button

/view

全局注册组件

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

这个组件,需要在每个页面引入;

组件代码:需要换个图片就可以用了;

template

view

viewid="_drag_button":style="{top:top+'px',left:left+'px',opacity:firstIn1:0}"

@touchstart="touchstart"@touchmove.stop.prevent="touchmove"@touchend="touchend"

@click.stop.prevent="click":

buttonopen-type="contact"

imagesrc="图片地址"

/image

/button

/view

/view

/template

script

exportdefault{

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(){

//获取手机信息配置接口

constsys=uni.getSystemInfoSync();

//屏幕的宽高

this.windowWidth=sys.windowWidth;

this.windowHeight=sys.windowHeight;

//#ifdefAPP-PLUS

this.existTabBar(this.windowHeight-=50);

//#endif

if(sys.windowTop){

this.windowHeight+=sys.windowTop;

//获取元素

constquery=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){

returnfalse;

console.log('移动',e);

this.isMove=true;

this.left=e.touches[0].clientX-this.offsetWidth;

letclientY=e.touches[0].clientY-this.offsetHeight;

//#ifdefH5

clientY+=this.height;

//#endif

letedgeBottom=this.windowHeight-this.height-this.edge;

//上下触及边界

if(clientYthis.edge){

this.top=this.edge;

}elseif(clientYedgeBottom){

this.top=edgeBottom;

}else{

this.top=clientY

uni.setStorageSync("top",this.top);

touchend(e){

if(this.isDock){

letedgeRigth=this.windowWidth-this.width-this.edge;

if(this.leftthis.windowWidth/2-this.offsetWidth){

this.left=this.edge;

}else{

this.left=edgeRigth;

uni.setStorageSync("left",this.left);

this.isMove=false;

this.$emit('btnTouchend');

/script

stylelang="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.3sease,top.3sease;

.btn{

background-color:transparent;

width:135rpx;

heigh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论