Home Web Front-end uni-app How to implement pull-down refresh and pull-up loading functions in uniapp

How to implement pull-down refresh and pull-up loading functions in uniapp

Apr 14, 2023 pm 03:34 PM

With the continuous upgrading of mobile terminal development, developers’ demand for mobile applications is also getting higher and higher. In many mobile applications, pull-down to refresh and pull-up to load are more essential functions. In order to improve user experience, many mobile applications will add these two functions. Here, we will introduce how to implement pull-down to refresh and pull-up to load more functions in uniapp.

1. Basic content introduction

uniapp is a cross-platform application development framework based on the Vue.js framework, which supports one-time writing and multi-end publishing. Due to its good cross-platform features and built-in many native APIs, developers can use these APIs directly in uniapp without having to learn other development languages.

In uniapp, pull-down refresh and pull-up loading are more built-in components, that is, <uni-scroll-view>, through this control we can realize pull-down refresh and pull-up Load functionality without involving other modules.

2. Pull-down refresh

Pull-down refresh means that when the page content is pulled down, an event is triggered, the latest data is obtained from the server, and the data of the current page is replaced. In this chapter, we will introduce how to use the <uni-scroll-view> component to implement the pull-down refresh function in uniapp.

  1. Open the page

Before developing the uniapp page, you first need to enter the project tool Hbuilder X and create a new uniapp project , since we want to implement the pull-down refresh and pull-up loading functions, we need to first confirm that we are using the uni-ui component library, so we must select uni-ui when creating a new project selection page.

  1. Write code

The following is the specific implementation method:

Note: The sample code is only for reference and does not guarantee 100% correctness .

index.vue code:

<!-- 下拉刷新 -->
<uni-scroll-view
    class="content"
    :enable-back-to-top="true"
    @downRefresh="onDownRefresh" 
    refresher-default-style
    :refresher-triggered="isRefreshing"
    refresher-loading="{{isLoading}}"
    refresher-enabled="{{true}}"
    style="height:100vh;"
>
    <!--该区域可以放置需要下拉刷新的内容-->
    <!--......-->
    <div class="list-view">
        <ul>
            <li v-for="item in items" :key="item.id">{{item.text}}</li>
        </ul>
    </div>
</uni-scroll-view>

<script>
export default {
    data() {
        return {
            items: [
                {text: 'item1', id:1},
                {text: 'item2', id:2},
                {text: 'item3', id:3},
                {text: 'item4', id:4},
                {text: 'item5', id:5},
                {text: 'item6', id:6},
                {text: 'item7', id:7},
                {text: 'item8', id:8},
                {text: 'item9', id:9},
                {text: 'item10', id:10}
            ],
            isRefreshing: false,
            isLoading: false,
        }
    },
    methods: {
        onDownRefresh() {
            this.isRefreshing = true;
            setTimeout(() => {
                this.isRefreshing = false;
                uni.stopPullDownRefresh()
            }, 2000)
        },
    }
}
</script>

<style>
.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
</style>
Copy after login

After the above steps, we can implement the pull-down refresh function. Specifically, it is implemented through the downRefresh event of the <uni-scroll-view> component. When the pull-down triggers this event, we set isRefreshing to true, the refreshed status is displayed at this time. After the time arrives, we set isRefreshing to false and close uni.stopPullDownRefresh().

3. Pull-up loading

Pull-up loading more means that when the page content is pulled upward, a function is triggered to obtain more data from the server and add it to the end of the page. In this chapter, we will introduce how to use the <uni-scroll-view> component in uniapp to achieve more pull-up loading functions.

  1. Writing code

The above code already includes the pull-down refresh function, so we only need to add the code for the pull-up loading function to the above code.

<uni-scroll-view
    class="content"
    :enable-back-to-top="true"
    @downRefresh="onDownRefresh" 
    refresher-default-style
    :refresher-triggered="isRefreshing"
    refresher-loading="{{isLoading}}"
    refresher-enabled="{{true}}"
    @scrolltolower="loadMore" 
    :onLoadmore="false"
    style="height: 100vh;"
>
    <!--该区域可以放置需要下拉刷新的内容-->
    <!--......-->
    <div class="list-view">
        <ul>
            <li v-for="item in items" :key="item.id">{{item.text}}</li>
        </ul>
        <!--上拉加载-->
        <div v-if="isLoadMore">
            <span>loading...</span>
        </div>
    </div>
</uni-scroll-view>

<script>
export default {
    data() {
        return {
            items: [
                {text: 'item1', id:1},
                {text: 'item2', id:2},
                {text: 'item3', id:3},
                {text: 'item4', id:4},
                {text: 'item5', id:5},
                {text: 'item6', id:6},
                {text: 'item7', id:7},
                {text: 'item8', id:8},
                {text: 'item9', id:9},
                {text: 'item10', id:10}
            ],
            isRefreshing: false,
            isLoading: false,
            isLoadMore: false,
        }
    },
    methods: {
        onDownRefresh() {
            this.isRefreshing = true;
            setTimeout(() => {
                this.isRefreshing = false;
                uni.stopPullDownRefresh()
            }, 2000)
        },
        loadMore() {
            this.isLoadMore = true;
            setTimeout(() => {
                // 模拟从服务器获取了10个新数据
                for (let i=0; i<10; i++) {
                    const item = {
                        id: this.items.length + i + 1,
                        text: `item${this.items.length + i + 1}`
                    };
                    this.items.push(item);
                }
                this.isLoadMore = false;
            }, 2000)
        }
    }
}
</script>

<style>
.content {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
</style>
Copy after login

As shown in the above code, we implement pull-up loading through the @scrolltolower event in the <uni-scroll-view> component. When the page content slides to the bottom, we call the this.loadMore() method, in which we can load more data and update the page.

Finally, we set true for isLoadMore, and the "loading" status is displayed on the page. After waiting for 2 seconds, add 10 new data to the items data and close the isLoadMore state.

4. Summary

Through the methods introduced in this article, we can quickly implement more pull-down refresh and pull-up loading functions in uniapp. If you haven’t tried this feature yet, you can follow the steps in this article to get started quickly and make your mobile app even better. If you have any questions or queries, please leave them in the comment area.

The above is the detailed content of How to implement pull-down refresh and pull-up loading functions in uniapp. For more information, please follow other related articles on the PHP Chinese website!

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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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 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 can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

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.

How can you use lazy loading to improve performance? How can you use lazy loading to improve performance? Mar 27, 2025 pm 04:47 PM

Lazy loading defers non-critical resources to improve site performance, reducing load times and data usage. Key practices include prioritizing critical content and using efficient APIs.

What are some common patterns for managing complex data structures in UniApp? What are some common patterns for managing complex data structures in UniApp? Mar 25, 2025 pm 02:31 PM

The article discusses managing complex data structures in UniApp, focusing on patterns like Singleton, Observer, Factory, and State, and strategies for handling data state changes using Vuex and Vue 3 Composition API.

How does UniApp handle global configuration and styling? How does UniApp handle global configuration and styling? Mar 25, 2025 pm 02:20 PM

UniApp manages global configuration via manifest.json and styling through app.vue or app.scss, using uni.scss for variables and mixins. Best practices include using SCSS, modular styles, and responsive design.

How do you handle the back button in UniApp? How do you handle the back button in UniApp? Mar 26, 2025 pm 11:07 PM

The article discusses handling the back button in UniApp using the onBackPress method, detailing best practices, customization, and platform-specific behaviors.

See all articles