原生微信小程序---在页面上可拖动的按钮

.wxml

<view class="content" style="top:{{buttonTop}}px;left:{{buttonLeft}}px" 
    catch:touchmove="buttonMove" bind:touchstart="buttonStart" catch:tap="toSquare">
    <image src="../../assets/images/square-icon.png" class="icon"></image>
</view>

.js

let startPoint = null
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    buttonTop: 0,
    buttonLeft: 0,
    windowHeight: 0,
    windowWidth: 0,
    minTop: 0
  },
  pageLifetimes: {
    show() {
    //   wx.getSystemInfo({
    //     success: (res) => {
    //     //   console.log(res, 'info')
    //       //获取到设备的宽高
    //       let h = res.windowHeight, w = res.windowWidth
    //       this.setData({
    //         windowHeight: h,
    //         windowWidth: w,
    //         /* buttonLeft: getStorage('buttonLeft') ? getStorage('buttonLeft') : w * 0.7,
    //         buttonTop: getStorage('buttonTop') ? getStorage('buttonTop') : h * 0.7 */
    //         buttonLeft: w * 0.8,
    //         buttonTop: h * 0.8,
    //         minTop: res.statusBarHeight 
    //       })
    //     }
    //   })
    }
  },
  ready () {
    wx.getSystemInfo({
        success: (res) => {
        //   console.log(res, 'info')
          //获取到设备的宽高
          let h = res.windowHeight, w = res.windowWidth
          this.setData({
            windowHeight: h,
            windowWidth: w,
            /* buttonLeft: getStorage('buttonLeft') ? getStorage('buttonLeft') : w * 0.7,
            buttonTop: getStorage('buttonTop') ? getStorage('buttonTop') : h * 0.7 */
            buttonLeft: w * 0.8,
            buttonTop: h * 0.8,
            minTop: res.statusBarHeight 
          })
        }
      })
  },


  /**
   * 组件的方法列表
   */
  methods: {
    buttonStart(e) {
        // console.log(e, 'buttonStart')
        startPoint = e.touches[0]
    },
    buttonMove(e) {
        //计算拖动后的坐标
        let endPoint = e.touches[e.touches.length - 1],
          disX = endPoint.clientX - startPoint.clientX,
          disY = endPoint.clientY - startPoint.clientY
        startPoint = endPoint
  
        let buttonTop = this.data.buttonTop + disY,
          buttonLeft = this.data.buttonLeft + disX
  
        //判断是否超出屏幕
        if (buttonLeft + 72 >= this.data.windowWidth) {
          buttonLeft = this.data.windowWidth - 72
        }
        if (buttonLeft <= 0) {
          buttonLeft = 0
        }
        if (buttonTop + 80 >= this.data.windowHeight) {
          buttonTop = this.data.windowHeight - 80
        }
        if (buttonTop <= this.data.minTop) {
          buttonTop = this.data.minTop
        }
  
        this.setData({
          buttonTop,
          buttonLeft
        })
        // console.log(disX, disY, '999')
    }
})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值