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

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

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

  },






})
相关推荐
2501_915918411 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售6 小时前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl7 小时前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域8 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_8 小时前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务9 小时前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠13 小时前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
不如摸鱼去1 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
意会1 天前
微信闪照小程序实现
前端·css·微信小程序
小白_ysf1 天前
uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
微信小程序·uni-app