搜尋
首頁web前端uni-app淺析uniapp中怎麼創建上拉加載下拉刷新組件

淺析uniapp中怎麼創建上拉加載下拉刷新組件

Dec 20, 2021 pm 02:30 PM
uniapp上拉加載下拉重新整理自訂元件

uniapp中怎么创建上拉加载下拉刷新组件?下面本篇文章给大家分享一个uniapp 自定义上拉加载下拉刷新组件的实现方法,希望对大家有所帮助!

淺析uniapp中怎麼創建上拉加載下拉刷新組件

该组件是结合uview框架写的,主要结合了里面的u-loadmore组件,可配置下拉刷新加载圈的颜色及背景色,暂无数据时的图等,突出的特点就是通过设置组件的高度,适配刘海屏iPhone,且支持嵌套在各种盒子中。

iPhone手机即使我们没有开启原生的下拉刷新,上拉加载,默认也可以进行下拉和上拉的动作,我们可以在配置文件中关闭 "(disableScroll": true )。

"globalStyle": {
    "disableScroll": true ,
    "navigationStyle": "custom", // 隐藏系统导航栏
    "navigationBarTextStyle": "white" 
},

组件最终实现效果图

淺析uniapp中怎麼創建上拉加載下拉刷新組件

淺析uniapp中怎麼創建上拉加載下拉刷新組件

淺析uniapp中怎麼創建上拉加載下拉刷新組件

了解整个页面的结构,计算准确的滚动组件的高度

淺析uniapp中怎麼創建上拉加載下拉刷新組件

配置项个别详解

    //暂无数据的类型,就是根据不同的场景展示不同的暂无数据的图片,
    _type:{
            default:'',
            type:String
    },
比如列表中暂无数据(_type:nodata)

淺析uniapp中怎麼創建上拉加載下拉刷新組件

消息列表中暂无数据(_type:nomsg)

淺析uniapp中怎麼創建上拉加載下拉刷新組件

    //除了标题栏和状态栏以外的高度
    otherHeight: {
            default: 0,
            type: Number
    },

otherHeight具体指页面中(不确定元素)的高度,不理解请看整个页面的结构图

组件使用

1、在根目录下创建components文件夹,定义全局组件,组件名建议xxx-功能.vue,例如safe-scrollbox.vue

2、注册为全局组件(page.json)

"easycom": {
    "autoscan": true, //是否开启自动扫描,开启后将会自动扫描符合components/组件名称/组件名称.vue目录结构的组件
    "safe-(.*)": "@/components/safe-$1.vue", // 匹配components目录下组件名称/组件名称内的vue文件
    "^u-(.*)": "@/uview-ui/components/u-$1/u-$1.vue"
},

3、页面内使用

<safe-scrollbox @lowerFun="lowerFun" @refreshFun="refreshFun" :otherHeight="otherHeight"
    _type="nodata" :list="sealListArr" :status="loadStatus" :isRefresh="isRefresh" bgColor="#fff">
    <view class="seal-list-container" slot="contBox">
            <sealList @showActionBox="showActionBox" :list="sealListArr" :loadings="loadings"></sealList>
    </view>
</safe-scrollbox>

淺析uniapp中怎麼創建上拉加載下拉刷新組件

组件完整代码

<template>
	<!-- 滚动组件外层的框-->
	<view class="">
		<!-- 滚动区域 -->
		<scroll-view class="scroll-component" :scroll-top="scrollTop" scroll-y="true"
			:style="{
					height: `calc(100vh - ${statusBarHeight}px - ${navbarHeight}px - ${otherHeight}rpx - env(safe-area-inset-bottom) )`,
				}"
			:lower-threshold="150"
			:refresher-triggered="triggered"
			:refresher-enabled="true"
			refresher-default-style="none"
			:refresher-threshold="20"
			@refresherpulling="onPulling"
			@scrolltoupper="upper" @scrolltolower="lower"
			@scroll="scroll"
			@refresherrefresh="refresh"
			@refresherrestore="restore">
			<!-- 下拉刷新提示 -->
			<u-loadmore status="loading" bgColor="#fff" v-if="triggered" :icon-color="activeColor" :color="activeColor" :load-text="refreshText" margin-top="30" margin-bottom="30"/>
			<slot name="contBox"> </slot>
			<!-- 暂无数据 -->
			<view class="no-data-box" v-if="_type&&list.length==0">
				<image src="../static/imgs/nodata.png" v-if="_type==&#39;nodata&#39;" mode="aspectFit"></image>
				<image src="../static/imgs/nofile.png" v-if="_type==&#39;nofile&#39;" mode="aspectFit"></image>
				<image src="../static/imgs/nomsg.png" v-if="_type==&#39;nomsg&#39;" mode="aspectFit"></image>
				<image src="../static/imgs/nospace.png" v-if="_type==&#39;nospace&#39;" mode="aspectFit"></image>
				<image src="../static/imgs/nofile.png" v-if="_type==&#39;nofile&#39;" mode="aspectFit"></image>
			</view>
			<!-- 上拉加载 -->
			<u-loadmore :status="status" :load-text="loadText" margin-top="30" margin-bottom="30"/>
		</scroll-view>
		<!-- 回到顶部 -->
		<view @tap="goTop" class="go-top" v-if="old.scrollTop>500">
			  <u-icon name="arrow-upward" color="#909399" size="56"></u-icon>
		</view>
	</view>
</template>
import { mapGetters } from &#39;vuex&#39;;
export default {
	props: {
		// 这个数组结合暂无数据的类型主要是控制暂无数据模块的展示与隐藏
		list: {
			default: [],
			type: Array
		},
		//暂无数据的类型
		_type: {
			default: &#39;&#39;,
			type: String
		},
		//控制上拉加载时提示 loadmore代表还可以继续上拉加载 nomore没有更多数据 loading 加载中
		status: {
			default: &#39;loadmore&#39;,
			type: String
		},
		//结合这个控制下拉刷新时加载圈的显示隐藏
		isRefresh: {
			default: false,
			type: Boolean
		},
		//除了标题栏和状态栏以外的高度
		otherHeight: {
			default: 0,
			type: Number
		},
		//下拉加载时加载圈的背景色
		bgColor: {
			default: &#39;&#39;,
			type: String
		},
		//加载中,上拉加载时,暂无更多数据时所提示的文案
		loadText: {
			default: {
				loadmore: &#39;轻轻上拉获取更多数据&#39;,
				loading: &#39;努力加载中...&#39;,
				nomore: &#39;暂无更多数据&#39;
			},
			type: Object
		}
	},
	computed: {
		triggered() {
			return this.isRefresh;
		},
		...mapGetters([&#39;activeColor&#39;, &#39;statusBarHeight&#39;, &#39;navbarHeight&#39;, &#39;skeletonColor&#39;])
	},
	data() {
		return {
			old: {
				scrollTop: 0
			},
			scrollTop: 0,
			refreshText: {
				loading: &#39;正在获取最新数据...&#39;
			} //刷新文案
		};
	},
	methods: {
		onPulling() {
			// 下拉
			this.$emit(&#39;refreshFun&#39;);
			// this.triggered = true; //下拉加载,先让其变true再变false才能关闭
		},
		// 自定义下拉刷新控件被下拉
		refresh(e) {},
		// 刷新重置
		restore() {
			// this.triggered = &#39;restore&#39;; // 需要重置
		},
		// 刷新终止
		onAbort() {
			// console.log("onAbort");
		},
		upper: function(e) {
			console.log(e);
		},
		// 上拉加载
		lower: function(e) {
			// console.log(&#39;上拉加载&#39;)
			this.$emit(&#39;lowerFun&#39;);
			// console.log(e)
			// this.status=&#39;loading&#39;
		},
		scroll: function(e) {
			this.old.scrollTop = e.detail.scrollTop;
		},
		goTop: function(e) {
			this.scrollTop = this.old.scrollTop;
			this.$nextTick(() => {
				this.scrollTop = 0;
			});
		}
	}
};
.scroll-component {
	width: 750rpx;
	overflow-y: scroll;
}
/deep/ .u-loading-circle {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80rpx;
	width: 750rpx;
}
.go-top {
	position: fixed;
	bottom: 208rpx;
	right: 0;
	z-index: 2;
	background-color: red;
	width: 100rpx;
	height: 100rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	background: #606266;
	border-radius: 50%;
}
.no-data-box {
	width: 750rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	padding-top: 20%;
	margin-bottom: 10%;
	image {
		max-width: 600rpx;
	}
}

推荐:《uniapp教程

以上是淺析uniapp中怎麼創建上拉加載下拉刷新組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金社区。如有侵權,請聯絡admin@php.cn刪除
您如何在不同平台(例如移動,Web)上調試問題?您如何在不同平台(例如移動,Web)上調試問題?Mar 27, 2025 pm 05:07 PM

本文討論了有關移動和網絡平台的調試策略,突出顯示了Android Studio,Xcode和Chrome DevTools等工具,以及在OS和性能優化的一致結果的技術。

哪些調試工具可用於Uniapp開發?哪些調試工具可用於Uniapp開發?Mar 27, 2025 pm 05:05 PM

文章討論了用於Uniapp開發的調試工具和最佳實踐,重點關注Hbuilderx,微信開發人員工具和Chrome DevTools等工具。

您如何為Uniapp應用程序執行端到端測試?您如何為Uniapp應用程序執行端到端測試?Mar 27, 2025 pm 05:04 PM

本文討論了跨多個平台的Uniapp應用程序的端到端測試。它涵蓋定義測試方案,選擇諸如Appium和Cypress之類的工具,設置環境,寫作和運行測試,分析結果以及集成

您可以在Uniapp應用程序中執行哪些不同類型的測試?您可以在Uniapp應用程序中執行哪些不同類型的測試?Mar 27, 2025 pm 04:59 PM

本文討論了針對Uniapp應用程序的各種測試類型,包括單元,集成,功能,UI/UX,性能,跨平台和安全測試。它還涵蓋了確保跨平台兼容性,並推薦Jes等工具

Uniapp中有哪些常見的性能反版?Uniapp中有哪些常見的性能反版?Mar 27, 2025 pm 04:58 PM

本文討論了UNIAPP開發中的共同績效抗模式,例如過度的全球數據使用和效率低下的數據綁定,並提供策略來識別和減輕這些問題,以提高應用程序性能。

您如何使用分析工具來識別uniapp中的性能瓶頸?您如何使用分析工具來識別uniapp中的性能瓶頸?Mar 27, 2025 pm 04:57 PM

本文討論了使用分析工具來識別和解決Uniapp中的性能瓶頸,重點是設置,數據分析和優化。

您如何在Uniapp中優化網絡請求?您如何在Uniapp中優化網絡請求?Mar 27, 2025 pm 04:52 PM

本文討論了在UNIAPP中優化網絡請求的策略,重點是減少延遲,實施緩存以及使用監視工具來增強應用程序性能。

如何優化Uniapp中的Web性能的圖像?如何優化Uniapp中的Web性能的圖像?Mar 27, 2025 pm 04:50 PM

本文討論了通過壓縮,響應式設計,懶惰加載,緩存和使用WebP格式來優化Uniapp中的圖像,以更好地進行Web性能。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),