微信小程序如何动态获取设备宽高

207 阅读1分钟

1、代码

<view class="page" style="height:{{windowHeight}};width:{{windowWidth}}" >

 2、data里定义:

data(){
    windowHeight: '',
    windowWidth: ''
}

3、获取设备的宽高:异步接口

 /**   * 生命周期函数--监听页面初次渲染完成   */  
onReady: function() {  
  var self = this    
//获取设备的高度   
 wx.getSystemInfo({   
   success(res) {    
    console.log(res)    
    self.setData({    
      windowHeight: res.screenHeight*2+‘rpx’,
      windowWidth: res.screenWidth*2+'rpx'
      })      
  console.log(self.data.windowHeight)  
    }  
  }); 
 },