Vue3骨架屏(Skeleton)

效果如下圖:在線預覽

在這里插入圖片描述
在這里插入圖片描述

APIs

參數說明類型默認值必傳
animated是否展示動畫效果booleantruefalse
button是否使用按鈕占位圖boolean | SkeletonButtonPropsfalsefalse
avatar是否顯示頭像占位圖boolean | SkeletonAvatarPropsfalsefalse
input是否使用輸入框占位圖boolean | SkeletonInputPropsfalsefalse
image是否使用圖像占位圖booleanfalsefalse
title是否顯示標題占位圖boolean | SkeletonTitlePropstruefalse
paragraph是否顯示段落占位圖boolean | SkeletonParagraphPropstruefalse
loadingtrue 時,顯示占位圖,反之則直接展示子組件booleantruefalse

SkeletonButtonProps Type

名稱說明類型必傳
shape指定按鈕的形狀‘default’ | ‘round’ | ‘circle’false
size設置按鈕的大小‘default’ | ‘small’ | ‘large’false
block將按鈕寬度調整為其父寬度的選項booleanfalse

SkeletonAvatarProps Type

名稱說明類型必傳
shape指定頭像的形狀‘circle’ | ‘square’false
size設置頭像占位圖的大小number | ‘default’ | ‘small’ | ‘large’false

SkeletonInputProps Type

名稱說明類型必傳
size設置輸入框的大小‘default’ | ‘small’ | ‘large’false

SkeletonTitleProps Type

名稱說明類型必傳
width設置標題占位圖的寬度number | stringfalse

SkeletonParagraphProps Type

名稱說明類型必傳
rows設置段落占位圖的行數number | stringfalse
width設置段落占位圖的寬度,若為數組時則為對應的每行寬度,反之則是最后一行的寬度number | string | Array<number|string>false

創建骨架屏組件Skeleton.vue

<script setup lang="ts">
import { computed } from 'vue'interface SkeletonButtonProps {shape?: 'default'|'round'|'circle' // 指定按鈕的形狀size?: 'default'|'small'|'large' // 設置按鈕的大小block?: boolean // 將按鈕寬度調整為其父寬度的選項
}
interface SkeletonAvatarProps {shape?: 'circle'|'square' // 指定頭像的形狀size?: number|'default'|'small'|'large' // 設置頭像占位圖的大小
}
interface SkeletonInputProps {size: 'default'|'small'|'large' // 設置輸入框的大小
}
interface SkeletonTitleProps {width?: number|string // 設置標題占位圖的寬度
}
interface SkeletonParagraphProps {rows?: number|string // 設置段落占位圖的行數width?: number|string|Array<number|string>	// 設置段落占位圖的寬度,若為數組時則為對應的每行寬度,反之則是最后一行的寬度
}
interface Props {animated?: boolean // 是否展示動畫效果button?: boolean|SkeletonButtonProps // 是否使用按鈕占位圖avatar?: boolean|SkeletonAvatarProps // 是否顯示頭像占位圖input?: boolean|SkeletonInputProps // 是否使用輸入框占位圖image?: boolean // 是否使用圖像占位圖title?: boolean|SkeletonTitleProps // 是否顯示標題占位圖paragraph?: boolean|SkeletonParagraphProps // 是否顯示段落占位圖loading?: boolean // 為 true 時,顯示占位圖,反之則直接展示子組件
}
const props = withDefaults(defineProps<Props>(), {animated: true,button: false,image: false,avatar: false,input: false,title: true,paragraph: true,loading: true
})
const buttonSize = computed(() => {if (typeof props.button === 'object') {if (props.button.size === 'large') {return 40}if (props.button.size === 'small') {return 24}return 32}
})
const titleTop = computed(() => {if (typeof props.avatar === 'boolean') {return 8} else {if (typeof props.avatar.size === 'number') {return (props.avatar.size - 16) / 2} else {const topMap = {default: 8,small: 4,large: 12}return topMap[props.avatar.size || 'default']}}
})
const titleWidth = computed(() => {if (typeof props.title === 'boolean') {return '38%'} else {if (typeof props.title.width === 'number') {return props.title.width + 'px'}return props.title.width || '38%'}
})
const paragraphRows = computed(() => {if (typeof props.paragraph === 'boolean') {return 3}return props.paragraph.rows
})
const paragraphWidth = computed(() => {if (typeof props.paragraph === 'boolean') {return Array(paragraphRows.value)} else {if (Array.isArray(props.paragraph.width)) {return props.paragraph.width.map((width: number|string) => {if (typeof width === 'number') {return width + 'px'} else {return width}})} else if (typeof props.paragraph.width === 'number') {return Array(paragraphRows.value).fill(props.paragraph.width + 'px')} else {return Array(paragraphRows.value).fill(props.paragraph.width)}}
})
</script>
<template><divv-if="loading":class="['m-skeleton', {'m-skeleton-avatar': avatar, 'm-skeleton-animated': animated}]":style="`--button-size: ${buttonSize}px; --title-top: ${titleTop}px;`"><spanv-if="button":class="['u-skeleton-button',{'u-button-round': typeof button !== 'boolean' && button.shape === 'round','u-button-circle': typeof button !== 'boolean' && button.shape === 'circle','u-button-sm': typeof button !== 'boolean' && button.size === 'small','u-button-lg': typeof button !== 'boolean' && button.size === 'large','u-button-block': typeof button !== 'boolean' && button.shape !== 'circle' && button.block,}]"></span><span:class="['u-skeleton-input',{'u-input-sm': typeof input !== 'boolean' && input.size === 'small','u-input-lg': typeof input !== 'boolean' && input.size === 'large',}]" v-if="input"></span><div class="m-skeleton-image" v-if="image"><svg viewBox="0 0 1098 1024" xmlns="http://www.w3.org/2000/svg" class="m-skeleton-image-svg"><path class="u-skeleton-image-path" d="M365.714286 329.142857q0 45.714286-32.036571 77.677714t-77.677714 32.036571-77.677714-32.036571-32.036571-77.677714 32.036571-77.677714 77.677714-32.036571 77.677714 32.036571 32.036571 77.677714zM950.857143 548.571429l0 256-804.571429 0 0-109.714286 182.857143-182.857143 91.428571 91.428571 292.571429-292.571429zM1005.714286 146.285714l-914.285714 0q-7.460571 0-12.873143 5.412571t-5.412571 12.873143l0 694.857143q0 7.460571 5.412571 12.873143t12.873143 5.412571l914.285714 0q7.460571 0 12.873143-5.412571t5.412571-12.873143l0-694.857143q0-7.460571-5.412571-12.873143t-12.873143-5.412571zM1097.142857 164.571429l0 694.857143q0 37.741714-26.843429 64.585143t-64.585143 26.843429l-914.285714 0q-37.741714 0-64.585143-26.843429t-26.843429-64.585143l0-694.857143q0-37.741714 26.843429-64.585143t64.585143-26.843429l914.285714 0q37.741714 0 64.585143 26.843429t26.843429 64.585143z"></path></svg></div><div class="m-skeleton-header" v-if="avatar"><span:class="['u-skeleton-avatar',{'u-avatar-sm': typeof avatar !== 'boolean' && avatar.size === 'small','u-avatar-lg': typeof avatar !== 'boolean' && avatar.size === 'large','u-avatar-square': typeof avatar !== 'boolean' && avatar.shape === 'square',}]"></span></div><template v-if="!button && !image && !input"><div class="m-skeleton-content"><h3 class="u-skeleton-title" :style="{ width: titleWidth }"></h3><ul class="u-skeleton-paragraph"><li v-for="n in paragraphRows" :key="n" :style="`width: ${paragraphWidth[(n as number) - 1]};`"></li></ul></div></template></div><slot v-else></slot>
</template>
<style lang="less" scoped>
.m-skeleton {display: table;width: 100%;.u-skeleton-button {display: inline-block;vertical-align: top;background: rgba(0, 0, 0, .06);border-radius: 4px;width: 64px;min-width: 64px;height: 32px;line-height: 32px;}.u-button-sm {width: 48px;min-width: 48px;height: 24px;line-height: 24px;}.u-button-lg {width: 80px;min-width: 80px;height: 40px;line-height: 40px;}.u-button-round {border-radius: var(--button-size);}.u-button-circle {width: var(--button-size);min-width: var(--button-size);border-radius: 50%;}.u-button-block {width: 100%;}.u-skeleton-input {display: inline-block;vertical-align: top;background: rgba(0, 0, 0, 0.06);border-radius: 4px;width: 160px;min-width: 160px;height: 32px;line-height: 32px;}.u-input-sm {width: 120px;min-width: 120px;height: 24px;line-height: 24px;}.u-input-lg {width: 200px;min-width: 200px;height: 40px;line-height: 40px;}.m-skeleton-image {display: flex;align-items: center;justify-content: center;vertical-align: top;background: rgba(0, 0, 0, .06);border-radius: 4px;width: 96px;height: 96px;line-height: 96px;.m-skeleton-image-svg {width: 48px;height: 48px;line-height: 48px;max-width: 192px;max-height: 192px;.u-skeleton-image-path {fill: #bfbfbf;}}}.m-skeleton-header {display: table-cell;padding-right: 16px;vertical-align: top;.u-skeleton-avatar {display: inline-block;vertical-align: top;background: rgba(0, 0, 0, .06);width: 32px;height: 32px;line-height: 32px;border-radius: 50%;}.u-avatar-sm {width: 24px;height: 24px;line-height: 24px;}.u-avatar-lg {width: 40px;height: 40px;line-height: 40px;}.u-avatar-square {border-radius: 6px;}}.m-skeleton-content {display: table-cell;width: 100%;vertical-align: top;.u-skeleton-title {margin: 0;height: 16px;background: rgba(0, 0, 0, .06);border-radius: 4px;}.u-skeleton-paragraph {margin-top: 24px;padding: 0;li {height: 16px;list-style: none;background: rgba(0, 0, 0, .06);border-radius: 4px;&:not(:first-child) {margin-top: 16px;}&:last-child {width: 61%;}}}}
}
.m-skeleton-avatar {.m-skeleton-content {.u-skeleton-title {margin-top: var(--title-top);}.u-skeleton-paragraph {margin-top: 28px;}}
}
.m-skeleton-animated {.u-skeleton-button,.u-skeleton-input,.m-skeleton-image,.m-skeleton-header .u-skeleton-avatar,.m-skeleton-content .u-skeleton-title,.m-skeleton-content .u-skeleton-paragraph li {position: relative;z-index: 0;overflow: hidden;background: transparent;&::after {position: absolute;top: 0;left: -150%;bottom: 0;right: -150%;background: linear-gradient(90deg, rgba(0, 0, 0, .06) 25%, rgba(0, 0, 0, .15) 37%, rgba(0, 0, 0, .06) 63%);animation-name: skeleton-loading;animation-duration: 1.4s;animation-timing-function: ease;animation-iteration-count: infinite;content: "";}@keyframes skeleton-loading {0% {transform: translateX(-37.5%);}100% {transform: translateX(37.5%);}}}
}
</style>

在要使用的頁面引入

<script setup lang="ts">
import Skeleton from './Skeleton.vue'
import { ref } from 'vue'const loading = ref<boolean>(false)const showSkeleton = () => {loading.value = truesetTimeout(() => {loading.value = false}, 2000)
}
const animated = ref(false)
const block = ref(false)
const size = ref('default')
const buttonShape = ref('default')
const avatarShape = ref('circle')
const sizeOptions = ref([{label: 'Default',value: 'default'},{label: 'Large',value: 'large'},{label: 'Small',value: 'small'}
])
const buttonShapeOptions = ref([{label: 'Default',value: 'default'},{label: 'Round',value: 'round'},{label: 'Circle',value: 'circle'}
])
const avatarShapeOptions = ref([{label: 'Square',value: 'square'},{label: 'Circle',value: 'circle'}
])
</script>
<template><div><h1>{{ $route.name }} {{ $route.meta.title }}</h1><h2 class="mt30 mb10">基本使用</h2><Skeleton /><h2 class="mt30 mb10">復雜的組合</h2><Skeleton avatar :paragraph="{ rows: 4 }" /><h2 class="mt30 mb10">包含子組件</h2><Button :loading="loading" @click="showSkeleton">Show Skeleton</Button><br/><br/><Skeleton :loading="loading"><div><h4>Vue Amazing UI, a design language</h4><br/><p>We supply a series of design principles, practical patterns and high quality designresources, to help people create their product prototypes beautifully and efficiently.</p></div></Skeleton><h2 class="mt30 mb10">自定義標題和段落</h2><Skeleton avatar :title="{ width: '24%' }" :paragraph="{ rows: 4, width: ['48%', '72%', '96%', '60%'] }" /><h2 class="mt30 mb10">按鈕 / 輸入框 / 圖像 / 頭像</h2><Flex :gap="32"><Flex vertical :gap="12" width="50%"><Skeleton :animated="animated" :button="{ shape: buttonShape, size: size, block: block}" /><Skeleton style="width: 200px" :animated="animated" :input="{ size: size }" /><Skeleton :animated="animated" image /><Skeleton :avatar="{ shape: avatarShape, size: size }" :paragraph="{ rows: 2 }" /></Flex><Flex vertical :gap="36" width="50%"><Space :size="32"><Space align="center">animated: <Switch v-model:checked="animated" /></Space><Space align="center">Button Block: <Switch v-model:checked="block" /></Space></Space><Space align="center">Size: <Radio :options="sizeOptions" v-model:value="size" /></Space><Space align="center">Button Shape: <Radio :options="buttonShapeOptions" v-model:value="buttonShape" /></Space><Space align="center">Avatar Shape: <Radio :options="avatarShapeOptions" v-model:value="avatarShape" /></Space></Flex></Flex></div>
</template>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/14595.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/14595.shtml
英文地址,請注明出處:http://en.pswp.cn/web/14595.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SOLIDWORKS二次開發服務商 慧德敏學

SOLIDWORKS是一套三維設計軟件, 采用特征建模、變量化驅動可方便地實現三維建模、裝配和生成工程圖。SOLIDWORKS軟件本身所具有的交互方式, 可以使用戶對已生成模型的尺寸、幾何輪廓和相互約束關系隨時進行修改, 而不需要編程。但要實現設計意義上的變量化繪圖和系列化設計, 需…

java-查詢字符串當中是否包含中文

文章目錄 前言java-查詢字符串當中是否包含中文 前言 如果您覺得有用的話&#xff0c;記得給博主點個贊&#xff0c;評論&#xff0c;收藏一鍵三連啊&#xff0c;寫作不易啊^ _ ^。 ??而且聽說點贊的人每天的運氣都不會太差&#xff0c;實在白嫖的話&#xff0c;那歡迎常來啊…

軟考系統架構師一些知識點記錄-1

個人隨筆 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu) 引言 準備去參加軟考的考試&#xff0c;但對一些概念掌握的還不夠&#xff0c;借此機會&#xff0c;整理記錄一二&#xff0c;便于自己理解掌握。 知識范圍 感覺不夠清晰的部分主要是第三篇和第四篇的部分。…

國際頂會認可!KaiwuDB 論文入選 ICDE 2024

導 讀 近日&#xff0c;KaiwuDB 與中國人民大學合作的論文 FOSS: A Self-Learned Doctor for Query Optimizer 被數據庫領域頂會The 40th IEEE International Conference on Data Engineering (ICDE 2024) 錄用啦! 論文中提出了具備自學習、自診斷能力的查詢優化器 FOSS&…

USB官方文檔怎么下載

直接登錄USB官網"https://usb.org/" 如&#xff0c;我需要查找與USB device class相關的文檔 點擊搜索后就能找到。 學習還是要以官方文檔為主&#xff0c;博客上的介紹不可信&#xff0c;USB協議規范很重要!

商品發布功能

文章目錄 1.SPU和SKU介紹1.SPU2.SKU3.兩者之間的關系 2.完成商品發布界面1.組件引入1.commoditylaunch.vue 引入到 src/views/modules/commodity下2.multiUpload.vue 引入到 src/components/upload/multiUpload.vue 2.創建菜單1.創建目錄2.創建菜單&#xff0c;注意菜單路由要匹…

go語言中同一for循環體內的多個初始變量和多個自增變量用法示例

在go語言的for循環體中&#xff0c;我們可以同時初始多個變量&#xff0c; 也可以同時多多個變量進行自增/自減操作&#xff0c; 用法如下&#xff1a; for 后面的多個初始化變量使用的是逗號分隔的批量賦值操作&#xff0c;多個變量自增自減使用 加減運算符和逗號分隔 字符…

MySQL之性能剖析和Schema與數據類型優化(一)

性能剖析總結 1.定義性能最有效的方法是響應時間2.如果無法測量就無法有效地優化&#xff0c;所以性能優化工作需要基于高質量、全方位及完整的響應時間測量3.測量的最佳開始點是應用程序&#xff0c;而不是數據庫。即使問題出在底層的數據庫&#xff0c;借助良好的測量也可以…

C++系列-友元

&#x1f308;個人主頁&#xff1a;羽晨同學 &#x1f4ab;個人格言:“成為自己未來的主人~” 我們在之前的文章有提到友元&#xff0c;我們先來看下面的這段包含了友元的代碼&#xff1a; ??#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace…

CLIP論文學習

學習來自B站bryanyzhu

jdk17安裝教程詳細(jdk17安裝超詳細圖文)

2021年9月14日JDK17 發布&#xff0c;其中不僅包含很多新語言功能&#xff0c;而且與舊版 JDK 相比&#xff0c;性能提升也非常明顯。與之前 LTS 版本的 JDK 8 和 JDK 11 相比&#xff0c;JDK17 的性能提升尤為明顯&#xff0c;本文將教你如何安裝 相比于JDK1.8&#xff0c;JD…

虛擬機網絡設置為橋接模式后未顯示網絡

本方法為&#xff0c;VMware配置正確&#xff0c;但在嘗試其他辦法后未能成功解決的人提供一種方法 本機的虛擬機使用NAT模式正常使用 但是使用橋接模式后重啟&#xff0c;未發現虛擬機內網絡設置,詳見下圖&#xff1a; 使用 ifconfig 查看網絡詳情 發現沒有ens33接口 查看硬…

雙非本科,逆襲中大廠的 Java 學習路線

從零基礎入門 Java&#xff0c;到最后秋招上岸&#xff0c;筆者也是花費了不少的經歷&#xff0c;也走了很多彎路。這一篇文章會記錄下真正有用的學習路線。 為什么要強調真正有用&#xff1f;網上的很多所謂從入門到求職&#xff0c;推薦的路線都超級長&#xff0c;零基礎的同…

LeetCode198:打家劫舍

題目描述 你是一個專業的小偷&#xff0c;計劃偷竊沿街的房屋。每間房內都藏有一定的現金&#xff0c;影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統&#xff0c;如果兩間相鄰的房屋在同一晚上被小偷闖入&#xff0c;系統會自動報警。 給定一個代表每個房屋存…

【學習筆記】Windows GDI繪圖(六)圖形路徑GraphicsPath詳解(中)

上一篇【學習筆記】Windows GDI繪圖(五)圖形路徑GraphicsPath詳解(上)介紹了GraphicsPath類的構造函數、屬性和方法AddArc添加橢圓弧、AddBezier添加貝賽爾曲線、AddClosedCurve添加封閉基數樣條曲線、AddCurve添加開放基數樣條曲線、基數樣條如何轉Bezier、AddEllipse添加橢圓…

華為校招機試 - 最久最少使用緩存(20240508)

題目描述 無線通信移動性需要在基站上配置鄰區(本端基站的小區 LocalCell 與周邊鄰基站的小區 NeighborCelI 映射)關系, 為了能夠加速無線算法的計算效率,設計一個鄰區關系緩存表,用于快速的通過本小區 LocalCell 查詢到鄰小區 NeighborCell。 但是緩存表有一定的規格限…

代碼隨想錄-Day07

454. 四數相加 II 給你四個整數數組 nums1、nums2、nums3 和 nums4 &#xff0c;數組長度都是 n &#xff0c;請你計算有多少個元組 (i, j, k, l) 能滿足&#xff1a; 0 < i, j, k, l < n nums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 輸入&#x…

系統磁盤高級管理、lvm例子、創建pv、創建VG、創建lv、磁盤擴展

LVM&#xff1a; 邏輯卷&#xff0c;動態調整分區大小&#xff0c;擴展性好 創建pv pvcreate &#xff1a; 將實體 partition 創建成為 PV &#xff1b; pvscan &#xff1a; 搜尋目前系統里面任何具有 PV 的磁盤&#xff1b; pvdisplay &#xff1a; 顯示出目前系統上面…

GNSS仿真測試之三種常用坐標系與轉換

作者介紹 在當今的全球導航衛星系統&#xff08;GNSS&#xff09;技術領域&#xff0c;仿真測試是評估和驗證GNSS接收機性能的關鍵環節&#xff0c;全球導航衛星系統&#xff08;GNSS&#xff09;仿真測試是確保GNSS接收機和導航解決方案在實際部署前能夠正確、可靠地工作的關鍵…

【git】學習記錄: 貯藏功能

Git 貯藏修改是一種臨時存儲工作目錄中已經修改但尚未提交的更改的機制。通過貯藏修改&#xff0c;你可以將當前的工作目錄狀態保存起來&#xff0c;以便你可以在之后的時間點重新應用這些更改&#xff0c;或者在不同的分支間切換時避免沖突。 要使用 Git 貯藏修改&#xff0c…