uni.getSystemInfo({
success: function (res) {
const windowWidth = res.windowWidth; // 窗口宽度
const windowHeight = res.windowHeight; // 窗口高度
console.log(windowWidth);
console.log(windowHeight);
}
});
在
uni.getSystemInfo
的回调函数中,通过res
参数可以获取到系统信息。其中的windowWidth
表示窗口宽度,windowHeight
表示窗口高度。您可以将它们保存到变量中或进行其他操作。
请注意,在使用
uni.getSystemInfo
方法时,需要将其放在适当的位置,如页面的onLoad
方法中,以确保在页面加载完成后获取到准确的窗口宽度和高度。
另外:
uni.onWindowResize(function (res) {
console.log(res.size.windowWidth); // 旋转后可使用窗口宽度
console.log(res.size.windowHeight); // 旋转后可使用窗口高度
})