uniapp項目實踐自定義加載組件示例詳解
準備工作
有時候一個頁面請求接口需要加載很長時間,這時候就需要一個加載頁面來告知用戶內容正在請求加載中,下面就寫一個簡單的自定義加載組件。
在之前的全局組件目錄components
下新建一個組件文件夾,命名為q-loading
,組件為q-loading.vue
。
再找?guī)讉€效果不錯的 css 加載動畫,然后修改一下樣式。
邏輯思路
編寫模板部分
要求具有擴展性,因此可以使用slot
插槽來插入內容,也方便后期修改自定義。
使用class
和style
綁定一些父組件傳過來的值,更加個性化。
這個頁面分為圖標和文本提示兩部分,各自可以自定義顯示、大小、顏色。
編寫樣式部分
這部分就是圖標和文本的樣式以及一些加載動畫的內容。
編寫腳本部分
這部分主要是父組件傳遞過來的參數(shù),通過props
進行制定格式。
實戰(zhàn)演練
下面就簡單實現(xiàn)一個加載組件。
模板部分
<view class="q-loading" :style="{'backgroundColor': props.bgColor}" v-if="props.show" > <view class="q-loading-inner"> <slot name="load"> <!-- 圖標部分 --> <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> <!-- 旋轉 --> <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>
樣式部分
這部分就是頁面的樣式以及三個對應的動畫。
- 頁面的樣式
.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; } } } } }
- 三個對應的動畫
// 圓環(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%); } } // 轉動 @keyframes loadingCircleRound { to { transform: rotate(1turn); } }
腳本部分
這部分就是傳遞的數(shù)據(jù),包括組件、圖標和文本的顯示控制,以及各自的顏色,大小等參數(shù)。
const props = defineProps({ // 顯示加載 show: { type: Boolean, default: true, }, // 背景色 bgColor: { type: String, default: "#fff", }, // 顯示圖標 showIcon: { type: Boolean, default: true, }, // 名稱 iconName: { type: String, default: "circle", }, // 大小 borSize: { type: Number, default: 60, }, // 邊框粗細 borWin: { type: Number, default: 8, }, // 顏色 borColor: { type: String, default: "#e3e3e3", }, // 活動顏色 bordActiveColor: { type: String, default: "#24afd6", }, // 顯示文本 showText: { type: Boolean, default: true, }, // 文本內容 text: { type: String, default: "加載中...", }, // 文本顏色 textColor: { type: String, default: "#555", }, // 文本大小 textSize: { type: Number, default: 20, }, });
效果預覽
下面看一下預覽效果吧。
圓環(huán)效果
呼吸效果
旋轉效果
最后
以上就是自定義加載組件的主要內容,更多關于uniapp自定義加載組件的資料請關注腳本之家其它相關文章!
相關文章
Javascript hasOwnProperty 方法 & in 關鍵字
hasOwnProperty :如果 object 具有指定名稱的屬性,那么方法返回 true;反之則返回 false。2008-11-11詳解Html a標簽中href和onclick用法、區(qū)別、優(yōu)先級別
本文主要分享一篇關于Html A標簽中href和onclick用法、區(qū)別、優(yōu)先級別,具有很好的參考價值,有需要了解的朋友可以看看2017-01-01淺談js對象屬性 通過點(.) 和方括號([]) 的不同之處
下面小編就為大家?guī)硪黄獪\談js對象屬性 通過點(.) 和方括號([]) 的不同之處。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10JS實現(xiàn)根據(jù)當前文字選擇返回被選中的文字
這篇文章主要介紹JS如何實現(xiàn)根據(jù)當前文字選擇返回被選中的文字,需要的朋友可以參考下2014-05-05