微信小程序 实现可拖动悬浮图标

效果图
在这里插入图片描述
(1)wxml

<view class="float-layout" bindtap="floatClick" catchtouchmove="buttonMove" bindtouchstart="buttonStart" bindtouchend="buttonEnd" style="top:{
   {buttonTop}}px;left:{
   {buttonLeft}}px;">
  <image class='float-img' src="../icon_service.png" mode="scaleToFill" />
  <view class="float-txt" >问题反馈</view>
</view>

(2)js


var startPoint
Page({
   
  data: {
   
    //按钮位置参数
    buttonTop: 0,
    buttonLeft: 0,
    windowHeight: '',
    windowWidth: '',
  },
  onLoad(options) {
   
    var that = this;
    wx.getSystemInfo({
   
      success: function (res) {
   
        // 高度,宽度 单位为px
        that.setData({
   
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          buttonTop: res.windowHeight * 0.50, //这里定义按钮的初始位置
          buttonLeft: res.windowWidth * 0.75, //这里定义按钮的初始位置
        })
      }
    })
  },
  /**
   * 可拖动悬浮按钮点击事件
   */
  floatClick: function () {
   
    wx.showToast({
   
      title: 'd点击了',
      icon: 'success',
      duration: 1000
    })
  },
  //以下是按钮拖动事件
  buttonStart: function (e) {
   
    startPoint = e.touches[0] //获取拖动开始点
  },
  buttonMove: function (e) {
   
    var endPoint = e.touches[e.touches.length - 1] //获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint //重置开始位置
    var buttonTop = this.data.buttonTop + translateY
    var 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) {
   }

})

(3)wxss

@import "../../public/wxss/base.wxss";
 /**可拖动悬浮按钮样式表**/
 .float-layout{
   
   position: fixed;
   padding: 15rpx 30rpx;
   background-color: rgba(255, 255, 255, 0.755);
   border-radius: 30%;
   display: flex;
   align-items: center;
   justify-content: center;
   z-index: 99999;
   box-shadow: 1px 1px 1px 1px #ede7e7;
   display: flex;
   flex-direction: column;
   justify-content: center;
   align-items: center;
 }
 .float-img{
   
  width: 75rpx;
  height: 75rpx;
}
 .float-txt{
   
   left:23%;
   top:27%;
   font-weight: 800;
   font-size: 32rpx;
   color: #3691FB;
 }

相关推荐

  1. 程序实现图片上传到服务器

    2023-12-22 23:24:03       38 阅读
  2. 程序实现图片下载与保存功能

    2023-12-22 23:24:03       86 阅读
  3. 程序实现图片转base64

    2023-12-22 23:24:03       10 阅读
  4. 程序图片资源优化实践

    2023-12-22 23:24:03       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 23:24:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 23:24:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 23:24:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 23:24:03       18 阅读

热门阅读

  1. 傅里叶变换全息图 Matlab

    2023-12-22 23:24:03       47 阅读
  2. Python---案例-多任务版TCP服务端程序开发

    2023-12-22 23:24:03       35 阅读
  3. LeetCode刷题(文章链接汇总)

    2023-12-22 23:24:03       47 阅读
  4. linux RCU 使用实例

    2023-12-22 23:24:03       31 阅读
  5. 卸载anaconda报错Error: Unable to move .anaconda

    2023-12-22 23:24:03       41 阅读
  6. Halcon DL-Model相关算子

    2023-12-22 23:24:03       33 阅读
  7. MATLAB中的协方差函数

    2023-12-22 23:24:03       34 阅读
  8. 楼宇对讲门铃的芯片构成分析

    2023-12-22 23:24:03       37 阅读
  9. 代码随想录 279. 完全平方数

    2023-12-22 23:24:03       34 阅读
  10. 详解Qml的底层实现

    2023-12-22 23:24:03       33 阅读