小程序全局拖动悬浮球插件

项目源码~~需要app.js设置全局变量,保证全局统一显示隐藏,以及移动位置。


  <movable-area class='fixed-area'>
    <scroll-view style='height:100%;' scroll-y='true' bindscroll="scrollPage" bindscrolltolower='toBottom'>
    <slot/>
    </scroll-view>
    <movable-view  wx:if="{{movableBlo}}" direction='all' x="{{x}}" y="{{y}}" class='ball' bindtouchstart='touchStart' bindtouchend='end' bindtap='returnMini' animation="{{animate}}" bindchange='change'>
      <image src='/image/return.png' class='return-icon'></image>
      <text class='return-text'>返回</text>
    </movable-view>
    <view class='close-area {{alreadyClose?"big":""}}' hidden="{{!closeArea}}" wx:if="{{movableBlo}}">
      <image src='/image/close-ball.png' class='close-icon'></image>
      <text class='close-text'>取消浮窗</text>
    </view>
  </movable-area>
page{
  height: 100%;
}
.fixed-area{
  width: 100%;
  height: 100%;
  -webkit-overflow-scrolling : touch;
}
.ball{
  position: fixed;
  background: #fff;
  box-shadow: 0 0 5px #ddd; 
  border: #eee;
  width:100rpx;
  height:100rpx;
  text-align: center;
  z-index: 1002;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.close-area{
  position: fixed;
  width: 500rpx;
  height: 500rpx;
  right: -250rpx;
  bottom: -250rpx;
  background: #eb5352;
  border-radius: 50%;
  z-index: 1001;
}
.big{
  right: -230rpx;
  bottom: -230rpx;
}

.close-icon{
  height: 70rpx;
  width: 70rpx;
  position: absolute;
  top: 90rpx;
  left: 110rpx;
}
.close-text{
  position: absolute;
  top: 185rpx;
  left: 98rpx;
  font-size: 24rpx;
  color: #fff;
}
.return-icon{
  width: 40rpx;
  height: 40rpx;
  display: block;
  margin-bottom: 3rpx;
}
.return-text{
  color: #ca7;
  font-size: 18rpx;
  display: block;
}
// assembly/returnBall/returnBall.js
const app = getApp();
Component({

  /**
   * 页面的初始数据
   */
  data: {
    x: app.globalData.returnBall.x,
    y: app.globalData.returnBall.y,
    movableBlo: app.globalData.returnBall.show,
    animate: false
  },
  methods: {
    change(e) {
      let screenWidth = this.data.screenWidth,
        screenHeight = this.data.screenHeight;
      let x = e.detail.x
      let y = e.detail.y
      if (x > screenWidth - 250 && y > screenHeight - 250) {
        this.setData({
          alreadyClose: true
        })
      } else {
        this.setData({
          alreadyClose: false
        })
      }
    },
    returnMini() {
      app.navigate.returnBack()
    },
    touchStart() {
      this.setData({
        closeArea: true
      })
    },
    end(e) {
      let screenWidth = this.data.screenWidth,
        screenHeight = this.data.screenHeight,
        x = e.changedTouches[0].pageX,
        y = e.changedTouches[0].pageY;
      if (x > screenWidth - 100 && y > screenHeight - 100) {
        app.globalData.returnBall.show = false
        this.setData({
          movableBlo: false
        })
      }
      setTimeout(() => {
        app.globalData.returnBall.x = screenWidth;
        app.globalData.returnBall.y = y;
        this.setData({
          animate: true,
          x: screenWidth,
          y,
          closeArea: false
        })
      }, 300)
    },
    scrollPage(e) {
      let pages = getCurrentPages();
      let curPage = pages[pages.length - 1];
      if (curPage.myPageScroll) {
        curPage.myPageScroll(e)
      }
    },
    toBottom() {
      let pages = getCurrentPages();
      let curPage = pages[pages.length - 1];
      if (curPage.toBottom) {
        curPage.toBottom()
      }
    }
  },
  pageLifetimes: {
    show() {
      this.setData({
        x: app.globalData.returnBall.x,
        y: app.globalData.returnBall.y,
        movableBlo: app.globalData.returnBall.show,
        animate: false
      })
      this.setData({
        screenWidth: wx.getSystemInfoSync().windowWidth,
        screenHeight: wx.getSystemInfoSync().windowHeight
      })
    },
  }
})
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值