当前位置: 首页 > news >正文

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

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

// 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

  .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

// 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()},})
http://www.lryc.cn/news/519223.html

相关文章:

  • Linux-蓝牙协议
  • moviepy 将mp4视频文件提取音频mp3 - python 实现
  • imageio 图片转mp4 保存mp4
  • Postman接口测试04|批量运行测试用例、参数化、Mock Server、Cookie鉴权、Newman生成测试报告
  • 学技术学英语:http状态码 401 Unauthorized vs 403 Forbidden
  • @LocalBuilder装饰器: 维持组件父子关系
  • React(二)——Admin主页/Orders页面/Category页面
  • 移动端屏幕分辨率rem,less
  • Docker Desktop 构建java8基础镜像jdk安装配置失效解决
  • 数据结构:栈(Stack)和队列(Queue)—面试题(一)
  • AR 眼镜之-拍照/录像动效切换-实现方案
  • 2025年中科院分区大类划分公布!新增8155本
  • S变换matlab实现
  • Springboot——钉钉(站内)实现登录第三方应用
  • 基于深度学习算法的AI图像视觉检测
  • cJson——序列化格式json和protobuf对比
  • 搭建一个fastapi的项目,调用ollama服务
  • Wireshark编译手册(Windows)
  • 在高德地图上加载3DTilesLayer图层模型/天地瓦片
  • 深入浅出负载均衡:理解其原理并选择最适合你的实现方式
  • STM32的存储结构
  • @SneakyThrows 注解详解
  • js监测页面可见性
  • Android wifi常见问题及分析
  • EFCore HasDefaultValueSql
  • Win10微调大语言模型ChatGLM2-6B
  • 什么叫区块链?怎么保证区块链的安全性?
  • 一、智能体强化学习——强化学习基础
  • 【DES加密】
  • .NET中的框架和运行环境