微信小程序可拖拽的按钮

先看成品

可以随意拖拽且不会超出屏幕的盒子

1、先随便写个固定定位到的盒子

注意盒子的距离屏幕左边和上边的距离用个全局变量代替,

方便拖拽的时候修改。

<view class="movebox" style="position: fixed;left: {{buttonLeft}}rpx;top: {{buttonHeight}}rpx;">
  <image src="../../aseets/海琴烟.jpg" mode="" class="moveImage"/>
</view>
data: {
    buttonLeft:567,
    buttonHeight:650
  },

2、使用微信小程序api获取设备的宽高(单位是px)

因为单位是px,我们如果做成自适应的,需要将px转换成rpx。

微信小程序屏幕宽度是750rpx

在data中定义数据

windowWidth:'',//设备的屏幕宽度
windowHeight:''//设备的屏幕高度
pixelRatio1:'',//px换算rpx

在onload中获取屏幕宽高()

onLoad(options) {
    const that=this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          pixelRatio1:750 / res.windowWidth//px换算成rpx
        })
      }
    })
  },

3、监听盒子的起始位置和终止位置

在position:fixed的盒子上加上小程序自带的监听事件

<view class="movebox" style="position: fixed;left: {{buttonLeft}}rpx;top: {{buttonTop}}rpx;" bindtouchmove="buttonMove" bindtouchstart="buttonStart">
  <image src="../../aseets/海琴烟.jpg" mode="" class="moveImage"/>
</view>

在最外侧注册一个起始变量

var startPoint;//起始点位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonLeft:567,
    buttonTop:650,
    windowWidth:'',//设备的屏幕宽度
    windowHeight:'',//设备的屏幕高度
    pixelRatio1:'',//px换算rpx

  },

对盒子的起始位置进行计算

buttonStart: function (e) {
      startPoint = e.touches[0]//盒子的起始位置
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]//盒子的结束位置
    var translateX = (endPoint.clientX - startPoint.clientX)*this.data.pixelRatio1//x轴移动的距离换算成rpx
    var translateY = (endPoint.clientY - startPoint.clientY)*this.data.pixelRatio1//y轴移动的距离换算成rpx
    startPoint = endPoint//这次是结束位置是下次的起始位置
    var buttonTop = this.data.buttonTop + translateY//起始的位置加上移动的距离就是现在的位置
    var buttonLeft = this.data.buttonLeft + translateX//起始的位置加上移动的距离就是现在的位置
    // console.log(buttonLeft,this.data.windowWidth*this.data.pixelRatio1);
    //判断是移动否超出屏幕
    // 如果左侧和上边超出屏幕则让其帖住屏幕
    if (buttonLeft <= 0) {
      buttonLeft = 0; 
    }
    if (buttonTop <= 0) {
      buttonTop = 0
    }
    // 判断右侧和底部是否超出屏幕,如果超出去则回退一个盒子的宽高
    if (buttonLeft + 160 >= this.data.windowWidth * this.data.pixelRatio1) {
      buttonLeft = this.data.windowWidth * this.data.pixelRatio1 - 160;
    }
    if (buttonTop + 90 >= this.data.windowHeight  * this.data.pixelRatio1) {
      buttonTop = this.data.windowHeight  * this.data.pixelRatio1- 90;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    })
  },

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤在微信小程序中实现点击按钮添加文本框并且点击文本框可以随意拖动的功能: 1. 在页面的 WXML 文件中,添加一个按钮和一个空的文本框容器: ```html <view> <button bindtap="addTextBox">添加文本框</button> <view class="text-box-container" bindtouchstart="startDrag" bindtouchmove="onDrag" bindtouchend="endDrag"> <input class="text-box" type="text" value="" /> </view> </view> ``` 2. 在页面的 WXSS 文件中,设置文本框容器的样式,使其可以拖动: ```css .text-box-container { position: absolute; left: 0; top: 0; width: 200rpx; /* 设置文本框容器的宽度 */ height: 100rpx; /* 设置文本框容器的高度 */ background-color: #eee; } ``` 3. 在页面的 JS 文件中,添加相应的事件处理函数和变量: ```javascript Page({ data: { textBoxes: [], // 存储文本框的数组 startX: 0, // 拖动开始时的横坐标 startY: 0, // 拖动开始时的纵坐标 currentIndex: -1, // 当前拖动的文本框索引 }, addTextBox() { const textBoxes = this.data.textBoxes; textBoxes.push({}); this.setData({ textBoxes, }); }, startDrag(e) { const currentIndex = e.currentTarget.dataset.index; this.setData({ startX: e.touches[0].clientX, startY: e.touches[0].clientY, currentIndex, }); }, onDrag(e) { const { startX, startY, currentIndex } = this.data; const offsetX = e.touches[0].clientX - startX; const offsetY = e.touches[0].clientY - startY; const textBoxes = this.data.textBoxes; textBoxes[currentIndex].left += offsetX; textBoxes[currentIndex].top += offsetY; this.setData({ startX: e.touches[0].clientX, startY: e.touches[0].clientY, textBoxes, }); }, endDrag() { this.setData({ currentIndex: -1, }); }, }); ``` 这样,当点击按钮后,会动态添加一个文本框。用户可以通过点击文本框并拖动来改变文本框的位置。请注意,上述代码仅提供了一个基本的实现思路,具体的样式和交互效果可以根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值