In the process of developing projects using uniapp, we often need to obtain the height of elements in order to perform corresponding operations and layout, such as dynamically calculating the height of list items, setting the minimum height of components, etc. So, how to get the height of the element in uniapp?
Method 1: uni.createComponent()
In uniapp, use uni.createComponent() to dynamically create custom components. In a custom component, we can use the uni.createSelectorQuery() method in the life cycle function of the custom component to obtain the information of the element node, including the height, width, etc. of the element.
Take getting the height of a div element as an example:
In the created life cycle function of the custom component, you can use the uni.createSelectorQuery() method to get the element information. The specific code is as follows:
<template> <div> <div> 我是一个自定义组件 </div> </div> </template> <script> export default { created () { // 获取元素的信息 uni.createSelectorQuery().in(this).select('.content').boundingClientRect((rect) => { console.log('元素高度为:' + rect.height) }).exec() } } </script> <style> .component { width: 100%; height: 100%; } .content { width: 100px; height: 100px; background-color: red; } </style>
In the above code, ref is used to obtain the reference of the div element, and then the uni.createSelectorQuery() method is used in the created life cycle function to query the element information. Among them, select('.content') means to query the element whose class is content, and the boundingClientRect() method means to query the size information of the element. The rect returned in the callback function is an object containing the position, size and other information of the element.
Method 2: uni.pageScrollTo()
In some cases, we need to get the height of an element on the page, which can be achieved using the uni.pageScrollTo() method. The specific code is as follows:
<template> <div> <div> 我是一个自定义组件 </div> </div> </template> <script> export default { mounted () { // 获取页面中元素的高度 uni.pageScrollTo({ selector: '.content', success: (res) => { console.log('元素高度为:' + res[0].top) } }) } } </script> <style> .component { width: 100%; height: 100%; } .content { width: 100px; height: 100px; background-color: red; } </style>
In the above code, the mounted life cycle function is used, and the uni.pageScrollTo() method is used after the page rendering is completed. selector: '.content' means querying elements with class content. The res in the success callback function can obtain the element information, where res[0].top means the distance between the element and the top of the page.
Summary:
The above two methods, the former is suitable for getting the height of elements in custom components, and the latter is suitable for getting the height of an element on the page. Both have their own advantages and disadvantages, and you can choose according to specific scenarios. No matter which method is used, you need to pay attention to adding the callback function of the size information within the corresponding life cycle function or method in order to obtain the height and other information of the element.
The above is the detailed content of How to get the height of an element in uniapp. For more information, please follow other related articles on the PHP Chinese website!

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment