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

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

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()

  },






})
相关推荐
默魔5 小时前
uniapp 微信小程序点击开始倒计时
微信小程序·小程序·uni-app
阿俊-全栈开发16 小时前
crmeb多门店对接拉卡拉支付小程序聚合收银台集成全流程详解
小程序·php·拉卡拉聚合收银台·拉卡拉三方支付
難釋懷1 天前
微信小程序全局配置
微信小程序·小程序
Enti7c1 天前
微信小程序核心知识点速览
微信小程序·小程序
2501_915918412 天前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域2 天前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷2 天前
第一个小程序
小程序
春哥的研究所2 天前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld2 天前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
今日热点2 天前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台