获取页面某元素高度:
const query = wx.createSelectorQuery()
query.select('.heihgtBody').boundingClientRect()
query.selectViewport().scrollOffset()
query.exec(function(res){
console.log(res)
}
)
获取屏幕高度:
onLoad(){
var that = this
console.log(wx.getSystemInfoSync())
let SystemInfoSync = wx.getSystemInfoSync()
let windowHeight = SystemInfoSync.windowHeight //屏幕高度
//部分手机额外底下的空白高度
let addedHeight = SystemInfoSync.screenHeight - SystemInfoSync.safeArea.bottom
const query = wx.createSelectorQuery()
query.select('.heihgtBody').boundingClientRect()
query.selectViewport().scrollOffset()
query.exec(function(res){
console.log("高度",res)
let height = windowHeight - res[0].height - 60 - addedHeight
that.setData({
swiperHeight: height + 'px'
})
})
},
使用 getComputedStyle() 方法来获取准确的元素高度
var items = this.refs.swiper.children //获取元素的节点
let height = window.getComputedStyle(items[0],null).height //得到 18.7656px
let num = parseFloat(height.substring(0,height.indexOf('px'))); // 得到18.7656
return num.toFixed(2) //得到18.77
px、rpx、rem间的转换:
var px = rpx / 750 * wx.getSystemInfoSync().windowWidth;
1 rpx = 屏幕宽度 / 750 px
1px= 1/375*100 vw
小程序的入口:
onLoad() {
const opt = wx.getLaunchOptionsSync()
switch (opt.scene) {
case 1001: // 发现栏小程序主入口,「最近使用」列表(基础库2.2.4版本起包含「我的小程序」列表)
case 1011: // 扫一扫二维码
case 1012: // 长按图片识别二维码
case 1013: // 扫描手机相册中选取的二维码
case 1017: // 前往小程序体验版的入口页
case 1025: // 扫描一维码
case 1026: // 发现栏小程序主入口,「附近的小程序」列表
case 1027: // 微信首页顶部搜索框搜索结果页「使用过的小程序」列表
case 1035: // 公众号自定义菜单
case 1047: // 扫描小程序码
case 1048: // 长按图片识别小程序码
case 1049: // 扫描手机相册中选取的小程序码
case 1053: // 搜一搜的结果页
uni.$u.route({
url: 'pages/index/find',
type: 'navigateTo'
})
break;
}
},
wx.getSystemInfo获取到的信息: