版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 结核性胸膜炎的护理与康复
- 角膜疾病的诊断与治疗方案
- 心脏疾病的早期识别与快速反应护理
- 心脏电风暴患者的心理护理与支持
- 家庭成员参与养老护理计划
- 应急安全团队建设方案
- 清理游泳池工作方案
- 延伸护理服务的模式与实践
- 寻找护理课件的最佳途径
- 肝移植适应症-洞察与解读
- 透析患者睡眠宣教
- 光纤通信 全套教案课件
- 施工安全生产管理体系方案
- 2025重庆渝贸通供应链管理有限责任公司招聘6人笔试备考试题及答案解析
- T-ZZB 2632-2022 化妆品级白油
- 钢管桩预应力锚索课件
- 学校“三风”建设中常见问题及措施
- 幼儿园区角环境创设现状与提升策略-以成都A幼儿园中班美工区为例
- 郑州城市职业学院《工业机器人》2024-2025学年第一学期期末试卷
- 餐饮预算方案模板(3篇)
- 2025年内蒙古党校在职研究生招生考试(政治理论)历年参考题库含答案详解(5套)
评论
0/150
提交评论