Home > WeChat Applet > Mini Program Development > How to get the height of an element in WeChat applet

How to get the height of an element in WeChat applet

angryTom
Release: 2020-03-24 15:06:42
Original
14138 people have browsed it

How to get the height of an element in WeChat applet

How to get the height of the element in the WeChat applet

1. Get the height of the element (px unit):

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

2. Get the height of the element (rpx unit), use the aspect ratio conversion to get: (the following 750 is the width of the element, the unit is 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();
Copy after login

3. After the page rendering is completed, OnReady callback, get When setting the height of an element, if no timer is added, the height of the element obtained is still the height before the asynchronous data is rendered. Therefore, you need to add a timer

onReady () {
    setTimeout(() => {
    let query = wx.createSelectorQuery();
    query.select('.content').boundingClientRect(rect=>{
        let height = rect.height;
        console.log(height);
        }).exec();
    }, 300)
}
Copy after login

PHP Chinese website, a large number of freesmall program development tutorials, welcome to learn!

The above is the detailed content of How to get the height of an element in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template