search
HomeWeb Front-enduni-appuniapp sets scroll height

uniapp sets scroll height

May 26, 2023 am 10:06 AM

Uniapp is a cross-platform application development tool based on the Vue.js framework, which can quickly build applications for multiple platforms (iOS, Android, H5). When developing with Uniapp, you often encounter the need to set the scroll height. Therefore, this article will introduce how to set the scroll height in Uniapp.

1. Set the page scroll height

  1. Add a scroll-view tag in the template

When writing the page, we can add it in the template A scroll-view tag to implement page scrolling. At the same time, we also need to set the height of the scroll-view so that the page can be scrolled.

For example, add the following code to the template:

<template>
  <scroll-view style="height: 1000rpx;">
    <!-- 页面内容 -->
  </scroll-view>
</template>

In this example, we set the height of scroll-view to 1000rpx.

  1. Calculate the scroll height in the page life cycle hook function

In addition to setting the height of scroll-view in the template, we can also set the scroll height in the page life cycle hook The scroll height is calculated in the function.

For example, in the onLoad hook function of the page, we can calculate the scroll height as follows:

<template>
  <scroll-view :style="{height: scrollHeight + 'rpx'}">
    <!-- 页面内容 -->
  </scroll-view>
</template>

<script>
  export default {
    data() {
      return {
        scrollHeight: 0
      };
    },
    onLoad() {
      // 获取屏幕高度
      const screenHeight = uni.getSystemInfoSync().screenHeight;
      // 计算scroll-view的高度
      const scrollHeight = screenHeight - 100;  // 100为非内容区高度
      // 更新scroll-view的高度
      this.scrollHeight = scrollHeight;
    }
  }
</script>

In this example, we use a variable scrollHeight to represent the height of scroll-view , its initial value is 0. In the onLoad hook function of the page, we obtained the height of the screen through the uni.getSystemInfoSync() API and calculated the height of the scroll-view based on the screen height. Finally, we assign the calculated scroll-view height to the scrollHeight variable, thereby updating the scroll height of the page.

2. Set the scroll height of the component

In addition to the page scroll height, we sometimes need to set the scroll height for the component. Uniapp provides a mixin mode to share code between multiple components. We can use the mixin pattern to set the scroll height of the component.

  1. Create a mixin

First, we need to create a mixin in the Uniapp project. Create a new "scrollHeightMixin.js" file in the "/common/mixins/" folder in the project root directory to store our mixin code.

export default {
  data() {
    return {
      scrollHeight: 0
    };
  },
  mounted() {
    // 获取屏幕高度
    const screenHeight = uni.getSystemInfoSync().screenHeight;
    // 计算scroll-view的高度
    const scrollHeight = screenHeight - 100;   // 100为非内容区高度
    // 更新scroll-view的高度
    this.scrollHeight = scrollHeight;
  }
};

In this example, we define a mixin named scrollHeightMixin, which contains a variable named scrollHeight. In the mounted hook function of the mixin, we use the uni.getSystemInfoSync() API to obtain the screen height, and calculate the height of the scroll-view based on the screen height. Finally, we assign the calculated scroll-view height to the scrollHeight variable to achieve the scroll height of the component.

  1. Using mixin in components

Next, we need to introduce the previously defined mixin into the component. Add the following code in the script tag of the component:

<script>
  import ScrollHeightMixin from "@/common/mixins/scrollHeightMixin.js";
  export default {
    mixins: [ScrollHeightMixin],
    // 组件其他内容
  };
</script>

In this example, we use the import statement to introduce the previously defined mixin into the component. Then, we add the mixin to the mixins property so that the component can use the scrollHeight variable defined in the mixin.

Finally, we can use the scroll-view tag in the component's template tag and set the height of the scroll-view to the scrollHeight variable value defined in the mixin.

<template>
  <scroll-view :style="{height: scrollHeight + 'rpx'}">
    <!-- 组件内容 -->
  </scroll-view>
</template>

In this example, we set the height of scroll-view to the scrollHeight variable value defined in the mixin.

Summary:

This article introduces how to use Uniapp to set the page scroll height and component scroll height. For the page scroll height, we can set the scroll-view height in the template, or calculate the scroll height in the page's life cycle hook function. For component scroll height, we can use mixin mode to achieve it. Hope this article is helpful to you in setting scroll height in Uniapp development.

The above is the detailed content of uniapp sets scroll height. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

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.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

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

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

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

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

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

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

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.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

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

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

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

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor