uni-app小程序获取自定义导航高和胶囊按钮定位的两个api
-
// 获取自定义导航的高
-
uni.getSystemInfo({
-
success: res => {
-
this.topHeight = res.statusBarHeight
-
}
-
})
-
console.log(this.topHeight);
在小程序平台,如果原生导航栏被隐藏,仍然在右上角会有一个悬浮按钮,微信下也被称为胶囊按钮。本API用于获取小程序下该菜单按钮的布局位置信息,方便开发者布局顶部内容时避开该按钮。
-
//获取胶囊按钮
-
-
let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
下面来尝试一下:
代码如下:【注释中为获取自定义导航的api】
-
export default {
-
components: {
-
bottomIcon
-
},
-
data() {
-
return {
-
titleheight: 0,
-
titletop:0,
-
}
-
},
-
onLoad() {
-
// 获取自定义导航的高
-
// uni.getSystemInfo({
-
// success: res => {
-
// this.topHeight = res.statusBarHeight
-
// }
-
// }),
-
this.getHeight();
-
// console.log("topHeight的值", this.topHeight);
-
-
},
-
methods: {
-
getHeight() {
-
let res = uni.getMenuButtonBoundingClientRect();
-
this.titletop = res.top;
-
this.titleheight = res.height
-
console.log(res);
-
console.log("titletop的值", this.titletop);
-
console.log("titleheight的值", this.titleheight);
-
console.log("一半的titleheight的值", this.titleheight/2);
-
}
-
-
}
-
}
-
</script>
在onLoad中调用一下this.getHeight();这个方法,得到返回值胶囊按钮距离顶部的top值以及自身的宽高,就可以用来自定义自己的导航栏,实现不同机型的兼容性。
-
//定义一下这个盒子的距离顶部的top值等于胶囊按钮的top
-
-
// 所以会在各个机型都保持与胶囊按钮在同一水平线
-
-
// 这里使用 v-bind 并没有使用插值语法{{titletop}}
-
-
<view class="back" :style="'top:' titletop 'px'">
-
-
</view>
-
-
-
.back{
-
position: absolute;//不写定位应该top不生效,可以自行试一下
-
}
还是挺有趣的。
如果是需要计算【顶部距离 胶囊按钮的高 底部空出间距】
-
<view class="top_view" :style="'margin-top:' (titletop titleheight 10) 'px'" >
-
-
</view>
放一张图就明白了:1、2、3分别对应 titletop 、 titleheigh 、 10
这样写出来兼容度较高。
自己复制粘贴用:
-
// view中 (添加此标签后 最上边的盒子正好与胶囊按钮平齐,可以加一个PX值让它向下移动距离)
-
-
:style="'margin-top:' (titletop titleheight 10) 'px'"
-
-
-
// data return中
-
-
titleheight: 0,
-
titletop:0,
-
-
-
// OnLoad中
-
-
this.getHeight();
-
-
//methods中
-
-
-
-
getHeight() {
-
let res = uni.getMenuButtonBoundingClientRect();
-
this.titletop = res.top;
-
this.titleheight = res.height
-
}
也可以简单封装一下:放入全局app.vue中:
-
<!-- 全局 -->
-
-
<script>
-
export default {
-
globalData() {
-
return {
-
titleheight: 0,
-
titletop: 0,
-
}
-
},
-
onLaunch: function() {
-
-
-
},
-
onLoad: function() {
-
-
},
-
onShow: function() {
-
console.log('App Show')
-
},
-
onHide: function() {
-
console.log('App Hide')
-
},
-
methods: {
-
getHeight() {
-
let res = uni.getMenuButtonBoundingClientRect();
-
this.titletop = res.top;
-
this.titleheight = res.height
-
},
-
},
-
-
}
-
</script>
-
-
<style>
-
/*每个页面公共css */
-
</style>
在需要的地方直接引入就行:【封装的并不好,暂且先用着。可以少写一个getHeight()方法】
-
<template>
-
-
<view> </view>
-
-
</template>
-
-
<script>
-
export default {
-
data(){
-
-
return{
-
titleheight:0,
-
titletop:0
-
}
-
},
-
-
onLoad() {
-
-
//通过全局vue得到getHeight()
-
getApp().getHeight();
-
this.titleheight = getApp().titleheight;
-
this.titletop = getApp().titletop;
-
},
-
-
methods: {
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgachgc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24