uniapp項(xiàng)目實(shí)踐自定義加載組件示例詳解
準(zhǔn)備工作
有時(shí)候一個(gè)頁(yè)面請(qǐng)求接口需要加載很長(zhǎng)時(shí)間,這時(shí)候就需要一個(gè)加載頁(yè)面來(lái)告知用戶內(nèi)容正在請(qǐng)求加載中,下面就寫一個(gè)簡(jiǎn)單的自定義加載組件。
在之前的全局組件目錄components
下新建一個(gè)組件文件夾,命名為q-loading
,組件為q-loading.vue
。
再找?guī)讉€(gè)效果不錯(cuò)的 css 加載動(dòng)畫,然后修改一下樣式。
邏輯思路
編寫模板部分
要求具有擴(kuò)展性,因此可以使用slot
插槽來(lái)插入內(nèi)容,也方便后期修改自定義。
使用class
和style
綁定一些父組件傳過(guò)來(lái)的值,更加個(gè)性化。
這個(gè)頁(yè)面分為圖標(biāo)和文本提示兩部分,各自可以自定義顯示、大小、顏色。
編寫樣式部分
這部分就是圖標(biāo)和文本的樣式以及一些加載動(dòng)畫的內(nèi)容。
編寫腳本部分
這部分主要是父組件傳遞過(guò)來(lái)的參數(shù),通過(guò)props
進(jìn)行制定格式。
實(shí)戰(zhàn)演練
下面就簡(jiǎn)單實(shí)現(xiàn)一個(gè)加載組件。
模板部分
<view class="q-loading" :style="{'backgroundColor': props.bgColor}" v-if="props.show" > <view class="q-loading-inner"> <slot name="load"> <!-- 圖標(biāo)部分 --> <view :class="{'q-loading-icon': true, 'pause': !props.show && !props.showIcon}" v-if="props.showIcon" > <slot name="icon"> <!-- 圓環(huán) --> <view class="q-loading-item q-loading-circle" :style="{'width': props.borSize +'rpx', 'height': props.borSize +'rpx', 'borderWidth': props.borWin + 'rpx', 'borderColor': props.borColor, 'borderLeftColor': props.bordActiveColor}" v-if="props.iconName == 'circle'" > </view> <!-- 呼吸 --> <view class="q-loading-item q-loading-circle-breath" v-if="props.iconName == 'circle-breath'" > </view> <!-- 旋轉(zhuǎn) --> <view class="q-loading-item q-loading-circle-round" v-if="props.iconName == 'circle-round'" > <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> <view class="loading-round" :style="{'backgroundColor': props.bordActiveColor}" ></view> </view> </slot> </view> <!-- 提示文本 --> <view class="q-loading-text" v-if="props.showText" :style="{'color': props.textColor, 'fontSize': props.textSize + 'rpx'}" > <slot name="text"> {{ props.text }} </slot> </view> </slot> </view> </view>
樣式部分
這部分就是頁(yè)面的樣式以及三個(gè)對(duì)應(yīng)的動(dòng)畫。
- 頁(yè)面的樣式
.q-loading { position: fixed; top: 0; left: 0; display: flex; justify-content: center; align-items: center; box-sizing: border-box; padding: 10rpx; width: 100%; height: 100vh; .q-loading-inner { display: flex; flex-direction: column; justify-content: center; align-items: center; .q-loading-icon { display: flex; justify-content: center; align-items: center; margin-bottom: 10rpx; width: 100rpx; height: 100rpx; .q-loading-circle { border-radius: 50%; border-style: solid; animation: loadingCircle 1s linear infinite; } .q-loading-circle-breath { box-shadow: 0 0 0 0 rgb(204, 73, 152); height: 36px; width: 36px; border-radius: 50%; animation: loadingCircleBreath 1s linear infinite; } .q-loading-circle-round { position: relative; width: 75rpx; height: 75rpx; .loading-round { position: absolute; width: 26rpx; height: 26rpx; border-radius: 50%; animation: loadingCircleRound 3s ease infinite; transform-origin: 120% 80rpx; &.loading-round:nth-child(1) { z-index: 7; } &.loading-round:nth-child(2) { height: 12px; width: 12px; animation-delay: 0.2s; z-index: 6; } &.loading-round:nth-child(3) { height: 11px; width: 11px; animation-delay: 0.4s; z-index: 5; } &.loading-round:nth-child(4) { height: 10px; width: 10px; animation-delay: 0.6s; z-index: 4; } &.loading-round:nth-child(5) { height: 9px; width: 9px; animation-delay: 0.8s; z-index: 3; } &.loading-round:nth-child(6) { height: 8px; width: 8px; animation-delay: 1s; z-index: 2; } &.loading-round:nth-child(7) { height: 7px; width: 7px; animation-delay: 1.2s; z-index: 1; } } } &.pause { .q-loading-item { animation-play-state: paused; } } } } }
- 三個(gè)對(duì)應(yīng)的動(dòng)畫
// 圓環(huán) @keyframes loadingCircle { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } } // 呼吸 @keyframes loadingCircleBreath { 0% { transform: scale(0.3); box-shadow: 0 0 0 0 rgba(36, 175, 214, 60%); } 60% { transform: scale(0.5); box-shadow: 0 0 0 56rpx rgba(36, 175, 214, 0%); } 100% { transform: scale(0.3); box-shadow: 0 0 0 0 rgba(36, 175, 214, 0%); } } // 轉(zhuǎn)動(dòng) @keyframes loadingCircleRound { to { transform: rotate(1turn); } }
腳本部分
這部分就是傳遞的數(shù)據(jù),包括組件、圖標(biāo)和文本的顯示控制,以及各自的顏色,大小等參數(shù)。
const props = defineProps({ // 顯示加載 show: { type: Boolean, default: true, }, // 背景色 bgColor: { type: String, default: "#fff", }, // 顯示圖標(biāo) showIcon: { type: Boolean, default: true, }, // 名稱 iconName: { type: String, default: "circle", }, // 大小 borSize: { type: Number, default: 60, }, // 邊框粗細(xì) borWin: { type: Number, default: 8, }, // 顏色 borColor: { type: String, default: "#e3e3e3", }, // 活動(dòng)顏色 bordActiveColor: { type: String, default: "#24afd6", }, // 顯示文本 showText: { type: Boolean, default: true, }, // 文本內(nèi)容 text: { type: String, default: "加載中...", }, // 文本顏色 textColor: { type: String, default: "#555", }, // 文本大小 textSize: { type: Number, default: 20, }, });
效果預(yù)覽
下面看一下預(yù)覽效果吧。
圓環(huán)效果
呼吸效果
旋轉(zhuǎn)效果
最后
以上就是自定義加載組件的主要內(nèi)容,更多關(guān)于uniapp自定義加載組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- uniapp自定義網(wǎng)絡(luò)檢測(cè)組件項(xiàng)目實(shí)戰(zhàn)總結(jié)分析
- uniapp實(shí)現(xiàn)滾動(dòng)觸底加載項(xiàng)目實(shí)戰(zhàn)
- uniapp自定義下拉刷新組件項(xiàng)目實(shí)踐總結(jié)分析
- uniapp封裝存儲(chǔ)和路由方法詳解
- uniapp項(xiàng)目實(shí)踐封裝通用請(qǐng)求上傳以及下載方法總結(jié)
- uniapp項(xiàng)目實(shí)踐之全局公共組件樣式及方法使用示例詳解
- uniapp自定義多列瀑布流組件項(xiàng)目實(shí)戰(zhàn)總結(jié)
相關(guān)文章
Javascript hasOwnProperty 方法 & in 關(guān)鍵字
hasOwnProperty :如果 object 具有指定名稱的屬性,那么方法返回 true;反之則返回 false。2008-11-11詳解Html a標(biāo)簽中href和onclick用法、區(qū)別、優(yōu)先級(jí)別
本文主要分享一篇關(guān)于Html A標(biāo)簽中href和onclick用法、區(qū)別、優(yōu)先級(jí)別,具有很好的參考價(jià)值,有需要了解的朋友可以看看2017-01-01淺談js對(duì)象屬性 通過(guò)點(diǎn)(.) 和方括號(hào)([]) 的不同之處
下面小編就為大家?guī)?lái)一篇淺談js對(duì)象屬性 通過(guò)點(diǎn)(.) 和方括號(hào)([]) 的不同之處。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10JS實(shí)現(xiàn)根據(jù)當(dāng)前文字選擇返回被選中的文字
這篇文章主要介紹JS如何實(shí)現(xiàn)根據(jù)當(dāng)前文字選擇返回被選中的文字,需要的朋友可以參考下2014-05-05