uniapp項目實踐自定義加載組件示例詳解
準備工作
有時候一個頁面請求接口需要加載很長時間,這時候就需要一個加載頁面來告知用戶內(nèi)容正在請求加載中,下面就寫一個簡單的自定義加載組件。
在之前的全局組件目錄components下新建一個組件文件夾,命名為q-loading,組件為q-loading.vue。
再找?guī)讉€效果不錯的 css 加載動畫,然后修改一下樣式。
邏輯思路
編寫模板部分
要求具有擴展性,因此可以使用slot插槽來插入內(nèi)容,也方便后期修改自定義。
使用class和style綁定一些父組件傳過來的值,更加個性化。
這個頁面分為圖標和文本提示兩部分,各自可以自定義顯示、大小、顏色。
編寫樣式部分
這部分就是圖標和文本的樣式以及一些加載動畫的內(nèi)容。
編寫腳本部分
這部分主要是父組件傳遞過來的參數(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>
<!-- 旋轉(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>樣式部分
這部分就是頁面的樣式以及三個對應的動畫。
- 頁面的樣式
.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%);
}
}
// 轉(zhuǎn)動
@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,
},
// 文本內(nèi)容
text: {
type: String,
default: "加載中...",
},
// 文本顏色
textColor: {
type: String,
default: "#555",
},
// 文本大小
textSize: {
type: Number,
default: 20,
},
});效果預覽
下面看一下預覽效果吧。
圓環(huán)效果

呼吸效果

旋轉(zhuǎn)效果

最后
以上就是自定義加載組件的主要內(nèi)容,更多關于uniapp自定義加載組件的資料請關注腳本之家其它相關文章!
相關文章
Javascript實現(xiàn)hashcode函數(shù)實現(xiàn)對象比較與原理說明
在JavaScript中,數(shù)值的比較是比較簡單的,使用相等(==)和全等(===)符號基本上可以解決大多數(shù)非對象的比較。但是相等(==)和全等(===)符號在對象 object 的比較上,就不能滿足所有的要求了2023-06-06
JavaScript在IE和Firefox瀏覽器下的7個差異兼容寫法小結
盡管那需要用長串的、沉悶的不同分支代碼來應付不同瀏覽器的日子已經(jīng)過去,偶爾還是有必要做一些簡單的區(qū)分和目標檢測來確保某塊代碼能在用戶的機器上正常運行。2010-06-06
bootstrap日歷插件datetimepicker使用方法
這篇文章主要為大家詳細介紹了bootstrap日歷datetimepicker插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

