微信原生小程序一键制作悬浮球

// back-home.js
Component({
    properties: {
      itemWidth: {
        type: Number,
        value: 64,
      },
      itemHeight: {
        type: Number,
        value: 64,
      },
      gapWidth: {
        type: Number,
        value: 10,
      },
      coefficientHeight: {
        type: Number,
        value: 0.72,
      },
    },
    data: {
      startX: 0,
      startY: 0,
      moveX: 0,
      moveY: 0,
      top: 0,
      left: 0,
      clientWidth: 0,
      clientHeight: 0,
    },
    attached() {
      const systemInfo = wx.getSystemInfoSync();
      this.setData({
        clientWidth:systemInfo.windowWidth,
        clientHeight:systemInfo.windowHeight
      })

      this.setData({
        left: this.data.clientWidth - this.data.itemWidth - this.data.gapWidth,
        top: this.data.clientHeight *this.data.coefficientHeight,
      });
    },
    methods: {
      handleTouchStart(e) {
          this.setData({
            startX:e.touches[0].clientX,
            startY:e.touches[0].clientY
          })
      },
      handleTouchMove(e) {
        const touch = e.touches[0];
        const offsetX = touch.clientX - this.data.startX;
        const offsetY = touch.clientY - this.data.startY;
        this.data.moveX = offsetX;
        this.data.moveY = offsetY;
        this.setData({
          left: this.data.left + offsetX,
          top: this.data.top + offsetY,
        });
        this.data.startX = touch.clientX;
        this.data.startY = touch.clientY;
      },
      handleTouchEnd() {
        this.adjustPosition();
        this.setData({
          moveX: 0,
          moveY: 0,
        });
      },
      goCreatePage() {
        this.triggerEvent('goManage');
      },
      adjustPosition() {
        if (this.data.left < 0  || this.data.left<this.data.clientWidth/3) {
          this.setData({ left: 0 });
        } else if (this.data.left > this.data.clientWidth - this.data.itemWidth - this.data.gapWidth || this.data.left>this.data.clientWidth/3) {
          this.setData({ left: this.data.clientWidth - this.data.itemWidth - this.data.gapWidth });
        }
  
        if (this.data.top < 0) {
          this.setData({ top: 0 });
        } else if (this.data.top > this.data.clientHeight - this.data.itemHeight - this.data.gapWidth) {
          this.setData({ top: this.data.clientHeight - this.data.itemHeight - this.data.gapWidth });
        }
      },
    },
  });
  

wxml文件

<!-- back-home.wxml -->
<view
  class="backHome"
  style="width: {{itemWidth}}px; height: {{itemHeight}}px; left: {{left}}px; top: {{top}}px;"
  bindtap="goCreatePage"
  bindtouchstart="handleTouchStart"
  bindtouchmove="handleTouchMove"
  bindtouchend="handleTouchEnd"
>
  <image src="../../assets/image/GoBook.png" mode="widthFix"></image>
</view>

wxss文件

/* back-home.wxss */
.backHome {
    position: fixed;
    z-index: 9;
  }
  
  .backHome image {
    width: 100%;
    height: 100%;
  }
  

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值