scroll-view填充屏幕剩余高度
scroll-view需要给一个固定高度,如果我们直接写死的话在不同设备的情况下有可能会超出屏幕高度或者短余屏幕高度,然后就封装了一个方法用来解决这个问题
方法:创建一个公共的js文件 方便复用
//第一个参数是要查询元素的class或者id 第二个要传递this
function getHeight(even, that) {
return new Promise((resolve, reject) => {
//拿到总高度
let windowHeight = 0;
wx.getSystemInfo({
success: (res => windowHeight = res.windowHeight)
});
//查询元素
let query = wx.createSelectorQuery().in(that).select(even).boundingClientRect();
//判断查询的元素是不是null,不是的话返回计算出来的高度
//由于我使用的单位是rpx 所以在总高度减去查询元素底部距顶距离之后*2
query.exec(res => (!res[0]) ? reject('没有查询到元素') : resolve((windowHeight - res[0].bottom) * 2))
})
}
module.exports = {
getHeight
}
使用:
//引入js
const getHeight = require("../../../utils/getRheRemainingHeight.js");
//根据实际情况 在生命周期或者函数中调用
getHeight.getHeight('.unread', this).then(res => {
this.setData({
height: res
})
})
页面中:
<!-- 被查询的元素 -->
<view class="unread">总共20个,有1个更新</view>
<!-- 测试方法 -->
<view style="height: {{height}}rpx; width: 150rpx; background: red;" />
这样就大功告成了