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

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

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

  },






})
相关推荐
说私域14 小时前
移动互联网生态下定制开发开源AI智能名片S2B2C商城小程序源码在营销技术中的应用与发展
人工智能·小程序·开源
微爱帮监所写信寄信14 小时前
微爱帮监狱写信寄信工具服务器【Linux篇章】再续:TCP协议——用技术隐喻重构网络世界的底层逻辑
linux·服务器·开发语言·网络·网络协议·小程序·监狱寄信
微爱帮监所写信寄信17 小时前
微爱帮监狱写信寄信小程序:MySQL核心日志与备份恢复安全架构
数据库·mysql·小程序·邮局·监狱寄信·挂号信·邮政
wan10418 小时前
用户隐私协议URL
微信小程序
Z单单19 小时前
微信小程序订单信息录入路径设置
微信小程序·小程序
码界奇点20 小时前
基于Spring Boot和微信小程序的小程序商城系统设计与实现
spring boot·微信小程序·小程序·毕业设计·源代码管理
微爱帮监所写信寄信20 小时前
微爱帮监狱寄信写信小程序:深入理解JavaScript中的Symbol特性
开发语言·javascript·网络协议·小程序·监狱寄信·微爱帮
计算机毕设指导621 小时前
基于微信小程序的智慧社区娱乐服务管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·娱乐
微爱帮监所写信寄信1 天前
6G+AI:重构微爱帮监狱写信寄信小程序的特殊通信未来
人工智能·小程序·重构·6g·监狱信件·监狱系统·服刑人员子女
赵庆明老师1 天前
uniapp 微信小程序页面JS模板
javascript·微信小程序·uni-app