微信小程序学习:(五)swiper塌陷问题解决

github地址: https://github.com/leoricding/-

(一)问题分析

需求:实现tab切换,内容切换;内容切换,tab也切换。

困难:实现的过程中,发现swiper-item无法撑开swiper

原因分析:小程序中,swiper-item是脱离文档流的,而swiper是固定高度,所以会出现无法撑开的问题。

解决方案:js动态计算swiper的高度。在swiper-item中填充srcoll-view,而srcoll-view是可以上下滚动的。所以,swiper高度=小程序可用高度-swiper意外其他元素的高度。

(二)不罗嗦直接上代码

<!--pages/lessionDetail/lessionDetail.wxml-->
<!-- 引用模板 -->
<import src="/pages/template/teacher/teacher.wxml" /><view class="container"><!-- 课程图片 --><view class="lession-img-container"><image class="lession-img" src="{{listData[index].carousel}}"></image></view><!-- tab切换标签 --><view class="tab-nav-container"><block wx:for="{{tabNavName}}" wx:key="{{index}}"><view class="tab-nav {{currentSelected==index?'selected':''}}"bindtap="selectTab" data-index="{{index}}"><text>{{item}}</text></view></block></view><!-- tab内容容器 --><view class="tab-content-container"><swiper  style="height: {{clientHeight?clientHeight+'px':'auto'}}"                 class="swiper"current="{{currentSelected}}"bindchange="changeTab"><swiper-item class="swiper-item"><scroll-view scroll-y="{{true}}" style="height: {{clientHeight?clientHeight+'px':'auto'}}" bindscrolltolower="scrollbot"><view class="swiper-item-content-container"><view class="lession-name"><text class="lession-name-text">{{listData[index].lessionName}}</text><view><text class="lession-name-detail">免费 订阅123 最近在学213 好评98%</text></view></view><!-- 老师介绍 --><view class="teacher-introduce"><text>老师介绍</text><template is="teacher" data="{{...listData[index]}}" /></view><!-- 课程介绍 --><view class="lession-detail"><text>课程介绍</text><view><text>{{listData[index].lessionDescription}}</text></view></view></view></scroll-view><view class="lession-buy-container"><view class="lession-buy"><text>¥{{listData[index].lessionPrice}}购买</text></view></view></swiper-item><swiper-item><text>这是课程目录</text></swiper-item><swiper-item><text>这是相关课程</text></swiper-item></swiper></view>
</view>
/* pages/lessionDetail/lessionDetail.wxss */
/* 引入模板样式 */
@import "/pages/template/teacher/teacher.wxss"
.container{display: flex;flex-direction: column;align-items: center;padding: 0;height: 100%;width: 100%;
}
/* 最上层课程图片 */
.lession-img-container{height: 400rpx;width: 100%;
}
.lession-img{height: 400rpx;width: 100%;
}/* tab切换 */
.tab-nav-container{width:100%;height: 60rpx;display: flex;flex-direction: row;justify-content: center;
}
.tab-nav{width: 33.3%;height: 100%;border-bottom: grey solid 1rpx;display: flex;flex-direction: row;justify-content: center;align-content: center;line-height: 60rpx;}
/* tab被选中时的样式 */
.tab-nav-container .selected{color:cyan;border-bottom:cyan solid 2rpx;
}
/* 这是tab切换对应的内容容器 */
.tab-content-container{width: 100%;
}/* 单个滑块容器 */
.swiper-item-content-container{display: flex;flex-direction: column;justify-content: center;
}/* 课堂名称栏 */
.lession-name{width: 100%;border-bottom: grey solid 1rpx;padding:20rpx;box-sizing: border-box;
}
.lession-name-text{font-size: 1.2rem;
}
.lession-name-detail{font-size: 0.6rem;color: grey;
}
/* 老师介绍栏 */
.teacher-introduce{height: 100%;padding: 20rpx;border-bottom: grey solid 1rpx;
}/* 课程介绍 */
.lession-detail{padding: 20rpx;/* border-bottom: grey solid 1rpx; */
}/* 底部购买课程 */
.lession-buy-container{position: absolute;bottom: 0;width: 100%;display: flex;flex-direction: row;justify-content: flex-end;border-top: grey solid 1rpx;background-color: white;z-index: 2;
}
.lession-buy{background-color: blue;line-height: 100%;font-size: 2rem;
}

(三)核心js代码

// pages/lessionDetail/lessionDetail.js
const app = getApp();
Page({/*** 组件的初始数据*/data: {listData:{},index:null,tabNavName:['详情',"目录","相关课程"],currentSelected:0,},onLoad:function(option){// 解决swiper塌陷问题// 思路:swiper高度=通过可用屏幕高度-课程图高度-tab栏高度let that=this;//测试获取指定元素属性(课程图的高度)wx.createSelectorQuery().selectAll('.lession-img-container').boundingClientRect(function (rect) {console.log(rect[0].height)//课程图高度imgHeightlet imgHeight = rect[0].height//获取tab栏的高度wx.createSelectorQuery().selectAll('.tab-nav-container').boundingClientRect(function (rect) {//tab栏高度rect[0].heightconsole.log(rect[0].height)//获取屏幕的可用高度wx.getSystemInfo({success: res => {// 解决swiper高度无法自适应的问题that.setData({//swiper高度clientHeightclientHeight: res.windowHeight - imgHeight - rect[0].height,});}})}).exec()  }).exec()}
})

(四)划重点

(1)获取元素高度

小程序api接口wx.createSelectorQuery() 创建查询节点,

.selectAll(’.lession-img-container’) 查询所有**.lession-img-container** 样式的元素

boundingClientRect(function (rect) {rect[0].height }) 回调函数,rect[0].height 就是我们所要的元素高度。

 wx.createSelectorQuery().selectAll('.lession-img-container').boundingClientRect(function (rect) { rect[0].height}).exec()

(2)获取可用屏幕高度

小程序api接口 wx.getSystemInfo 回调函数,获取系统信息。

res.windowHeight 就是屏幕的可用高度。

     //获取屏幕的可用高度wx.getSystemInfo({success: res => {res.windowHeight});}})

微信小程序学习:(五)swiper塌陷问题解决相关推荐

  1. 微信小程序学习(五)

    转载请注明出处:https://blog.csdn.net/Strugglein/article/details/81916136 今天带来小程序第四节的学习内容,今天主要学习目标是: 1.JS 2. ...

  2. 微信小程序学习(五):使用本地缓存,完成默认登录、、alert的使用、、引用全局变量,app.jsh中的变量

    微信小程序学习(五):使用本地缓存,完成默认登录 一开始想在app.js里面通过全局变量来实现默认登录状态的,但是没有用,每次重启还是要登录,在网上找了些资料后,可以用本地缓存, 这个是js文件 这是 ...

  3. 小程序 pagescrollto_微信小程序学习笔记(三)-- 首页及详情页开发

    一.常用组件 在上一个章节中讲解了封装请求数据的模块,在此处请求轮播图的数据 1.首页轮播图数据的请求以及渲染 1.1 轮播图数据的请求 pages/home/home.js import 2 使用组 ...

  4. 微信小程序学习笔记(1)

    微信小程序学习笔记 1.小程序代码结构 2.逻辑层和视图层 3. 小程序的宿主环境(通信模型.运行机制.组件.API) 4. 数据绑定和事件绑定 1.小程序代码结构 当开发者新建一个工程时,项目文件包 ...

  5. 2020微信小程序学习报告.2.17-3.1.(三)

    微信小程序学习报告应学校课程要求,特此记录首先,小程序知识点: wx.request的使用:发https请求,一个小程序同时只能有5个网络请求,https的参数里,url是接口地址,method是请求 ...

  6. 微信小程序学习(1)-基础开发

    学习微信小程序 微信小程序学习(1) 微信小程序学习(2) 文章目录 学习微信小程序 注册和初始化 小程序配置 tabbar导航栏 模板插样与WXML 循环渲染 条件渲染 模板 微信小程序脚本WXS ...

  7. 微信小程序学习(加深)

    微信小程序学习(加深) 一.wx:if 与 hidden 的对比 wx:if 以动态创建和移除元素的方式,控制元素的展示与隐藏 hidden 以切换样式的方式(display: none/block; ...

  8. 微信小程序学习实录1(wxml文档、引入weui、双向数据绑定、提交表单到后端)

    微信小程序学习实录 一.wxml文档 二.新建页面快捷方式 三.微信小程序引入weui 四.双向数据绑定 1.wxml渲染层 2.js逻辑层 提交表单到后端 五.微信小程序跳转到H5 一.wxml文档 ...

  9. 微信小程序学习11:iconfont 网络字体图标使用(阿里巴巴)

    微信小程序学习11:iconfont 网络字体图标使用(阿里巴巴) 使用方法 [1] 是直接下载图片,使用<image src="/static/images/v2.jpg" ...

最新文章

  1. 企业网络推广专员浅析企业网络推广后期网站优化重点因素有哪些?
  2. mysql忘记root密码恢复
  3. 扔掉工具类,Mybatis一个简单配置搞定数据加密解密!
  4. 织梦同步静态文件到服务器,某猫织梦插件-织梦dedecms静态文件生成速度的区块插件...
  5. 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]
  6. SpringBoot xml层SQL update之foreach循环的坑
  7. echart同一个dom下多次动态渲染值,防止值、事件重复互相影响
  8. html页面 request,HTML DOM requestFullscreen() 方法
  9. Python和C++交互
  10. html 城市选择 按字母排序吗,微信小程序实现按字母排列选择城市功能
  11. python查找相似图片
  12. 华润微CS88M312单片机带ADC 的8位低功耗MCU
  13. 过滤 Excel 表格内容中的无效字符
  14. 深度学习降噪方案-RNNoise简介和环境配置
  15. oracle 判断重复次数,sql 查询 某字段 重复次数 最多的记录
  16. 电脑端如何访问手机SD卡中的文件
  17. 最安全的现货白银建仓技巧有哪些?
  18. 你了解CPU吗?(三)
  19. Hi3518ev200:内核启动分析
  20. AKG K371 正确的佩戴方式,避免产生音漏降低低频

热门文章

  1. 一次函数的概念、性质及函数图象特征
  2. 游戏高级场景关卡设计师(深圳)
  3. Windows Server 2008 R2 JAVA jstack 命令报“存储空间不足,无法处理此命令”问题原因
  4. android+模拟器断开,启动后模拟器自动断开连接
  5. 手把手教你搭建一个中式菜谱知识图谱可视化系统
  6. SQL语句优化,看到性能瓶颈。
  7. 服务器项目怎么控标,控标参数.doc
  8. 单龙芯3A3000-7A1000PMON研究学习-(19)撸起袖子干-再来一杯代码3
  9. 网络分析仪基础(续)
  10. 升压、升降压开关电源设计的专用DC-DC控制器芯片 车载,安防,应急电源 详细解析与参数原理