線上體驗地址https://jsfiddle.net/z13wtr0q/ css
先說優缺點,畢竟骨架屏實現的方案有不少種html
經過僞元素實現骨架樣式,經過操做樣式實現骨架和頁面的動態切換vue
經過after
僞元素生成骨架樣式,並經過absolute
覆蓋到實際元素上react
.skt-loading { pointer-events: none; /* 加載中阻止事件 */ .skeleton { position: relative; overflow: hidden; border: none !important; border-radius: 5px; background-color: transparent !important; background-image: none !important; &::after { content: ''; position: absolute; left: 0; top: 0; z-index: 9; width: 100%; height: 100%; background-color: #EBF1F8; display: block; } /* 下面這部分都是自定義的,看需求修改 */ &:not(.not-round)::after { border-radius: 4px; } &:not(.not-before)::before { position: absolute; top: 0; width: 30%; height: 100%; content: ""; background: linear-gradient(to right,rgba(255,255,255,0) 0, rgba(255,255,255,.3) 50%,rgba(255,255,255,0) 100%); transform: skewX(-45deg); z-index: 99; animation: skeleton-ani 1s ease infinite; display: block; } &.badge { &::after { background-color: #F8FAFC; } } } } @keyframes skeleton-ani { /* 骨架屏動畫 */ from { left: -100%; } to { left: 150%; } }
只須要在你認爲合理的骨架粒度元素上添加skeleton
類便可dom
控制好skt-loading
類的切換動畫
歡迎各位拋磚spa