100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信小程序之可拖动悬浮按钮的实现

微信小程序之可拖动悬浮按钮的实现

时间:2022-03-19 08:51:09

相关推荐

微信小程序之可拖动悬浮按钮的实现

微信小程序之可拖动悬浮按钮的实现

什么都不说先看效果图

下面上代码

wxml

<button catchtouchmove="buttonMove" catchtouchstart="buttonStart" catchtouchend="buttonEnd" style="top:{{buttonTop}}px;left:{{buttonLeft}}px;width: 50px;height:50px;border-radius:50px;position:fixed;background:#ff6700;line-height:50px;font-size:25pt;color:#fff;">+</button>

js

var startPoint;Page({data: {buttonTop: 0,buttonLeft: 0,windowHeight: '',windowWidth: ''},onLoad: function (options) {var that =this;wx.getSystemInfo({success: function (res) {console.log(res);// 屏幕宽度、高度console.log('height=' + res.windowHeight);console.log('width=' + res.windowWidth);// 高度,宽度 单位为pxthat.setData({windowHeight: res.windowHeight,windowWidth: res.windowWidth})}})},onShow: function () {},buttonStart: function (e) {startPoint = e.touches[0]},buttonMove: function (e) {var endPoint = e.touches[e.touches.length - 1]var translateX = endPoint.clientX - startPoint.clientXvar translateY = endPoint.clientY - startPoint.clientYstartPoint = endPointvar buttonTop = this.data.buttonTop + translateYvar buttonLeft = this.data.buttonLeft + translateX//判断是移动否超出屏幕if (buttonLeft+50 >= this.data.windowWidth){buttonLeft = this.data.windowWidth-50;}if (buttonLeft<=0){buttonLeft=0;}if (buttonTop<=0){buttonTop=0}if (buttonTop + 50 >= this.data.windowHeight){buttonTop = this.data.windowHeight-50;}this.setData({buttonTop: buttonTop,buttonLeft: buttonLeft})},buttonEnd: function (e) {}})

ok,完成,很简单就不唠叨了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。