微信小程序 手机屏幕宽度高度
时间: 2023-06-30 07:07:08 浏览: 68
微信小程序可以通过wx.getSystemInfoSync()方法获取当前设备的系统信息,包括屏幕宽度和高度。具体代码如下:
```
const systemInfo = wx.getSystemInfoSync()
const screenWidth = systemInfo.screenWidth
const screenHeight = systemInfo.screenHeight
```
其中,screenWidth表示屏幕宽度,screenHeight表示屏幕高度,单位都是px。
相关问题
微信小程序 手机屏幕rpx宽度、rpx高度
在微信小程序中,屏幕的宽度和高度可以用rpx(responsive pixel)来表示。rpx是一种自适应的像素单位,它会根据屏幕宽度进行自动缩放,使得不同尺寸的屏幕上显示的大小保持一致。
要获取屏幕的rpx宽度和高度,可以通过wx.getSystemInfoSync()方法获取系统信息,其中包含了屏幕的宽度和高度等信息。然后可以通过以下公式计算得到rpx宽度和高度:
```
// 获取系统信息
const systemInfo = wx.getSystemInfoSync();
// 屏幕宽度,单位px
const screenWidth = systemInfo.screenWidth;
// 屏幕高度,单位px
const screenHeight = systemInfo.screenHeight;
// 可使用窗口宽度,单位px
const windowWidth = systemInfo.windowWidth;
// 可使用窗口高度,单位px
const windowHeight = systemInfo.windowHeight;
// rpx比例
const rpxRatio = 750 / windowWidth;
// 屏幕宽度,单位rpx
const screenWidthRpx = screenWidth / rpxRatio;
// 屏幕高度,单位rpx
const screenHeightRpx = screenHeight / rpxRatio;
```
其中,750是设计稿的宽度,可以根据实际情况进行调整。计算出来的screenWidthRpx和screenHeightRpx就是屏幕的rpx宽度和高度了。
微信小程序获取手机屏幕高度
可以通过微信小程序的API获取手机屏幕的高度,可以使用wx.getSystemInfoSync()方法获取设备信息,包括屏幕高度和宽度等信息。具体代码如下:
```javascript
// 获取设备信息
const sysInfo = wx.getSystemInfoSync()
// 获取屏幕高度
const screenHeight = sysInfo.windowHeight
```
获取屏幕宽度也可以通过类似的方法获取,只需要将`windowHeight`替换为`windowWidth`即可。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)