1. 程式人生 > >微信小程式 獲取元素高度(獲取元素節點資訊)

微信小程式 獲取元素高度(獲取元素節點資訊)

微信小程式 獲取元素高度(獲取元素節點資訊)

 

如果高度要px單位的話:

let query = wx.createSelectorQuery();
query.select('.content').boundingClientRect(rect=>{
  let height = rect.height;
  console.log(height);
}).exec();

 

如果高度要rpx單位的話,那麼可以用寬高比換算獲得:(以下的750是該元素的寬度,單位是rpx的)

let query = wx.createSelectorQuery();
query.select('.content').boundingClientRect(rect=>{
  let clientHeight = rect.height;
  let clientWidth = rect.width;
  let ratio = 750 / clientWidth;
  let height = clientHeight * ratio;
  console.log(height);
}).exec();

 

關於獲取螢幕視口高度,請檢視該文章:《微信小程式 簡單獲取螢幕視口高度