Home  >  Article  >  Web Front-end  >  How to modify the drop-down refresh style in Uniapp

How to modify the drop-down refresh style in Uniapp

PHPz
PHPzOriginal
2023-04-17 10:30:203365browse

In Uniapp, pull-down refresh is a very common function, but the default pull-down refresh style may not meet the application's UI design needs. Therefore, we need to modify the drop-down refresh style. This article will introduce how to modify the drop-down refresh style in Uniapp.

First of all, pull-down refresh in Uniapp is achieved by using the uni-scroll-view component. Therefore, we need to use this component to make pull-down refresh modifications.

The following are the specific steps to modify the pull-down refresh style of uni-scroll-view:

Step 1: Add the uni-scroll-view component in the template

In the template Add the uni-scroll-view component and add various attributes needed for pull-down refresh.

<template>
  <view>
    <uni-scroll-view class="scroll-view"
      :scroll-top="scrollTop"
      @scrolltolower="scrollToLower"
      @scroll="scroll"
      @refresh="refresh"
      :scroll-with-animation="scrollWithAnimation"
      :enable-back-to-top="enableBackToTop"
      :refresher-enabled="true"
      :refresher-threshold="45"
      :refresher-default-style="refresherDefaultStyle"
      :refresher-background-color="refresherBackgroundColor"
      :text-style="textStyle">

      <!-- 下拉刷新的容器组件 -->
      <view class="refresh-container">
        <view v-if="isRefreshing" class="loading-box">
          <loading class="loading" type="circular" size="23"></loading>
          <text class="loading-text">正在刷新...</text>
        </view>
        <view v-else class="arrow-icon-box">
          <image :src="arrowIconSrc" class="arrow-icon" :style="{transform: pullDownStyle}" />
          <text class="refresh-text">{{ refreshText }}</text>
        </view>
      </view>

      <!-- 加载更多的容器组件 -->
      <slot></slot>
      <view v-if="isLoadingMore" class="loading-more">{{ loadingMoreText }}</view>
    </uni-scroll-view>
  </view>
</template>

In the above template, we have used the refresher-enabled attribute and set it to true, thereby enabling the pull-down refresh feature.

Step 2: Add drop-down refresh style in style

Add various styles needed for drop-down refresh in style.

<style>
  .refresh-container {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    height: 45px;
    line-height: 45px;
    color: #999;
  }

  .arrow-icon-box {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    height: 45px;
    line-height: 45px;
  }

  .arrow-icon {
    width: 23px;
    height: 23px;
  }

  .refresh-text {
    margin-left: 12px;
    font-size: 14px;
  }

  .loading-box {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 45px;
    line-height: 45px;
    color: #999;
  }

  .loading {
    margin-left: 12px;
    margin-right: 12px;
  }

  .loading-text {
    font-size: 14px;
  }
</style>

In the above style, we have modified the styles of drop-down refresh container components, arrow icons, refresh text, loading text and other elements.

Step 3: Add pull-down refresh data and events in the script

Add the data and events needed for pull-down refresh in the script.

<script>
  export default {
    data() {
      return {
        isRefreshing: false,
        refreshText: '下拉刷新',
        arrowIconSrc: 'static/img/arrow-down.png',
        pullDownStyle: 'rotate(0deg)',
      }
    },
    methods: {
      // 下拉刷新事件
      refresh() {
        this.isRefreshing = true;
        this.refreshText = '正在刷新';
        this.arrowIconSrc = 'static/img/loading.gif';
        this.pullDownStyle = 'rotate(360deg)';
        setTimeout(() => {
          this.isRefreshing = false;
          this.refreshText = '下拉刷新';
          this.arrowIconSrc = 'static/img/arrow-down.png';
          this.pullDownStyle = 'rotate(0deg)';
        }, 2000);
      },
      // 滚动事件
      scroll(e) {
        // 滚动时记录滚动位置
        this.scrollTop = e.detail.scrollTop;
      },
      // 滚动到底部事件
      scrollToLower() {
        if (!this.isLoadingMore) {
          this.isLoadingMore = true;
          this.loadingMoreText = '加载中...';
          setTimeout(() => {
            this.isLoadingMore = false;
            this.loadingMoreText = '上拉加载更多';
          }, 2000);
        }
      },
    },
  }
</script>

In the above event, we implemented functions such as pull-down to refresh and scroll to the bottom to load more.

Summary

The above is all about how to modify the drop-down refresh style in Uniapp. Through the above steps, we can customize the pull-down refresh style and implement the pull-down refresh function. Hope this tutorial is helpful to you.

The above is the detailed content of How to modify the drop-down refresh style in Uniapp. 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