w***@163.com
w***@163.com
  • 发布:2024-07-05 11:44
  • 更新:2024-07-05 15:21
  • 阅读:203

请教指点:计算组件内元素的高度,在小程序端好用,在APP端不好使

分类:uni-app

计算组件内元素的高度,在小程序端好用,在APP端不好使。代码是第三方给开发的,用的vue2,我摘了出来,问AI、用了各种方法都不好使。。。前来请教

在小程序端好用


到了APP端就不好使了
下边有文件压缩包

wiki-container组件代码如下
<template name="">
<view class="wiki-container">
<view style="height:60px;background: #efefef;">标题标题标题标题标题</view>
<view>高度:{{navHeight}}</view>
<scroll-view ref="svRef" scroll-y class="sv" style="background: #c30;" :style="{height:navHeight+'px'}">
<slot name="container"></slot>
</scroll-view>
</view>
</template>

<script>
export default {
name: "wiki-container",
data() {
return {
pH: 0,
navHeight: 0
};
},
watch: {
hasBottomFixed(val) {
let that = this;
uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
success(res) { //成功回调函数
that._data.pH = res.windowHeight //windoHeight为窗口高度,主要使用的是这个
let titleH = uni.createSelectorQuery().in(that).select(".sv"); //想要获取高度的元素名(class/id)
titleH.boundingClientRect(data => {
let pH = that._data.pH;
if (that.hasBottomFixed) {
that._data.navHeight = pH - data.top - 60
//计算高度:元素高度=窗口高度-元素距离顶部的距离(data.top)
} else {
that._data.navHeight = pH - data.top //计算高度:元素高度=窗口高度-元素距离顶部的距离(data.top)
// Vue.prototype.wikiContentScrollHeight = pH - data.top
}
}).exec()
}
})
}
},
onReady() {
let that = this;
uni.getSystemInfo({ //调用uni-app接口获取屏幕高度
success(res) { //成功回调函数
that._data.pH = res.windowHeight //windoHeight为窗口高度,主要使用的是这个
let titleH = uni.createSelectorQuery().in(that).select(".sv"); //想要获取高度的元素名(class/id)
titleH.boundingClientRect(data => {
let pH = that._data.pH;
if (that.hasBottomFixed) {
that._data.navHeight = pH - data.top - 60
//计算高度:元素高度=窗口高度-元素距离顶部的距离(data.top)
} else {
that._data.navHeight = pH - data.top //计算高度:元素高度=窗口高度-元素距离顶部的距离(data.top)
}
}).exec()
}
})
},
};
</script>

2024-07-05 11:44 负责人:无 分享
已邀请:
DCloud_UNI_yuhe

DCloud_UNI_yuhe

你这个onReady,在小程序里面会生效,在app不会生效,你只需要把onReady改成mounted ,就正常啦

要回复问题请先登录注册