赞
踩
当一个小程序页面,顶部有搜索栏,或者分类查询时,我们想要保证它们能固定到顶部,就需要使用到Scrollview,那么如何确定Scrollview就是一个问题,这时我们可以使用以下代码来实现
- setScrollHeight(view = '#scrollView', name = 'scrollHeight') {
- let that = this
- uni.getSystemInfo({
- success: function(res) {
- const query = uni.createSelectorQuery().in(that).select(view);
- query.boundingClientRect(data => {
- that[name] = res.windowHeight - data.top
- }).exec();
- }
- });
- },
先获取Scrollview距离顶部的高度,然后页面高度减去顶部距离,就能得到剩余的内容高度,如果底部还有操作条,那么我们再减去其高度,就能得到具体的Scrollview高度了
注意点是为了能准确的获取到页面元素,从而准确计算高度,我们需要将代码放到onReady生命周期里运行
- onReady() {
- this.setScrollHeight()
- },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。