微信小程序实现拖拽盒子效果

要实现一个当前盒子高度由里面的盒子进行支配高度拖拽的效果

bash 复制代码
// wxml
   <view class="exmation-item" wx:elif="{{type==4}}">
<view class="exmation-item-drag-box"  id="drag-box"> 
      <!-- 内容 -->
      <view class="exmation-item-main"  style="height: {{topHeight}}px; ">
       
      </view>
      <!-- 拖动    -->
      <view class="exmation-item-box"   style="height: {{bottomHeight}}px;" >
<view class="exmation-item-box-top"   
bindtouchstart="onTouchStart" 
bindtouchmove="onTouchMove" 
bindtouchend="onTouchEnd" > 
</view>
      </view>
    </view>
    </view>

less

bash 复制代码
  .exmation-item-drag-box {
        height: calc(100vh - 90rpx - 88rpx - constant(safe-area-inset-bottom));
        height: calc(100vh - 90rpx - 88rpx -  env(safe-area-inset-bottom));
        overflow: hidden;
        .exmation-item-main{
         overflow-y: auto;
          height: 50%;
          margin-bottom: 0;
        }
        .exmation-item-box{
          height: 50%;
          background: #fff;
          .exmation-item-box-top{
            height: 100rpx;
          background: #000;
          }
        }
      }

js

bash 复制代码
// pages/exmation/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalHeight: 0, // 大盒子的总高度(可根据需要调整)
    topHeight: 0, // 上面盒子的初始高度
    bottomHeight: 0, 
    startY: 0, // 触摸开始时的Y坐标
    dragging: false, // 是否正在拖动
  },
  // 获取当前页面的高度
  getHeight() {
    const query = wx.createSelectorQuery();
    query.select('#drag-box').boundingClientRect( (rect) =>{
      // rect.height 就是盒子的高度
      console.log(rect.height );
      // 你可以将高度设置到data中以便后续使用
     this.setData({ totalHeight: rect.height,
      topHeight:rect.height/2,
      bottomHeight:rect.height/2
     });
    }).exec();
   
  },
 
  // 拖拽方法
  onTouchStart(e) {
    this.setData({
      startY: e.touches[0].clientY,
      dragging: true,
    });
  },
 
  onTouchMove(e) {
    if (!this.data.dragging) return;
    const moveY = e.touches[0].clientY;
    const deltaY = moveY - this.data.startY;
    let minheight = 50;
    let height=this.data.bottomHeight - deltaY;
    if(height>this.data.totalHeight)return;
    let newBottomHeight = (height < minheight ? minheight : height) 
       const newTopHeight = this.data.totalHeight - newBottomHeight;
      this.setData({
        startY: moveY,
        topHeight: newTopHeight>0?newTopHeight:0,
        bottomHeight: newBottomHeight>0?newBottomHeight:0,
      });
  },
 
  onTouchEnd() {
    this.setData({
      dragging: false,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  this.getHeight()

  },






})
相关推荐
CHU72903521 分钟前
定制专属美丽时刻:美容预约商城小程序的贴心设计
前端·小程序
hnxaoli7 小时前
统信小程序(十)nutika打包elf格式程序
小程序
CHU7290357 小时前
家门口的邻里集市:社区团购小程序的功能探索
小程序
hnxaoli7 小时前
统信小程序(十一)快捷地址栏
linux·python·小程序
职豚求职小程序10 小时前
中国人保财险笔试如何通过?附刷题库小程序
小程序
chushiyunen11 小时前
python轻量级框架flask、做桌面小程序
python·小程序·flask
笨笨狗吞噬者11 小时前
【uniapp】小程序支持分包存放微信自定义组件 wxcomponents
前端·微信小程序·uni-app
蓝色心灵-海12 小时前
小律书 技术架构详解:前后端分离的自律管理系统设计
java·http·小程序·架构·uni-app
00后程序员张14 小时前
iPhone 无需越狱文件管理 使用Keymob查看导出文件
android·ios·小程序·https·uni-app·iphone·webview
毕设源码-朱学姐14 小时前
【开题答辩全过程】以 基于微信小程序的运动减肥管理系统设计与实现为例,包含答辩的问题和答案
微信小程序·小程序