当前位置:   article > 正文

uniapp微信小程序获取屏幕宽高,胶囊按钮的位置以及Vue3.2在css层获取逻辑层的数据_uniapp 获取屏幕宽度

uniapp 获取屏幕宽度

场景:在开发小程序时需要把使用了固定定位的按钮放在屏幕的中间,考虑了用 vw  vh  % 但是还要减去按钮宽的一半,所以在这里不适用。以下是uniapp中自带的获取屏幕的高宽等数据,我在这里顺便记录一些其他小知识

1.使用uni.getWindowInfo() 

uniapp官网介绍: uni.getWindowInfo()

uni.getWindowInfo() 使用:

  1. // 获取窗口信息
  2. let getWindowInfo = uni.getWindowInfo()
  3. console.log(getWindowInfo.screenHeight);//屏幕高度
  4. console.log(getWindowInfo.screenWidth);//屏幕宽度
  5. console.log(getWindowInfo.windowHeight);//可操作页面高度
  6. console.log(getWindowInfo.windowWidth);//可操作页面宽度
  7. console.log(getWindowInfo);
  8. console.log('获取窗口信息');

uni.getSystemInfo() 使用:

  1. // 系统信息的概念
  2. uni.getSystemInfo({
  3. success: res => {
  4. console.log(res);
  5. console.log(res.screenHeight);//屏幕高度
  6. console.log(res.screenWidth);//屏幕宽度
  7. console.log(res.windowHeight);//可操作页面高度
  8. console.log(res.windowWidth);//可操作页面宽度
  9. }
  10. })

项目中应用场景还原:

逻辑层代码:

const screenWidths = ref<string>(uni.getWindowInfo().screenWidth - 180 + 'rpx')

css层代码:

  1. .add-address {
  2. position: fixed;
  3. bottom: 80rpx;
  4. left: v-bind(screenWidths);
  5. width: 360rpx;
  6. height: 72rpx;
  7. background: #101010;
  8. border-radius: 36rpx;
  9. font-size: 28rpx;
  10. font-family: PingFang SC-Medium, PingFang SC;
  11. font-weight: 500;
  12. color: #FFFFFF;
  13. line-height: 72rpx;
  14. text-align: center;
  15. }

实现效果:

 v-bind介绍

是vue3.2中增加了一个style v-bind的特性,简单来说就是把我们script中的数据可以在style标签中使用

2.获取胶囊按钮的位置信息 getMenuButtonBoundingClientRect()   

uniapp官网介绍: getMenuButtonBoundingClientRect()

getMenuButtonBoundingClientRect() 使用

  1. let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  2. console.log(menuButtonInfo.width) //宽度,单位:px
  3. console.log(menuButtonInfo.height) //高度,单位:px
  4. console.log(menuButtonInfo.top) //上边界坐标,单位:px
  5. console.log(menuButtonInfo.right) //右边界坐标,单位:px
  6. console.log(menuButtonInfo.bottom) //下边界坐标,单位:px
  7. console.log(menuButtonInfo.left) //左边界坐标,单位:px

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42731
推荐阅读
相关标签
  

闽ICP备14008679号