HTML & CSS:手把手教你实现骨架屏效果

效果演示

这个 HTML 和 CSS 代码创建了一个模拟加载效果的卡片,包含一个主加载条和两个辅助的加载条,用来模拟数据加载的过程。(骨架屏: 在需要等待加载内容的位置设置一个骨架屏,某些场景下比 Loading 的视觉效果更好。)

HTML

html 复制代码
<div class="card">
    <div class="card_load"></div>
    <div class="card_load_extreme_title"></div>
    <div class="card_load_extreme_descripion"></div>
</div>
  • card:作为卡片的容器,包含三个子元素:card_load、card_load_extreme_title和card_load_extreme_descripion。
  • card_load:代表主加载条,显示在卡片的最左侧。
  • card_load_extreme_title和card_load_extreme_descripion:分别代表卡片标题和描述的加载条,显示在卡片的右侧。

CSS

css 复制代码
.card {
  width: 190px;
  height: 90px;
  background: #ffff;
  box-shadow: 0 1px 25px rgba(0,0,0,0.2);
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  padding: 12px 10px;
  border-radius: 5px;
}

.card_load {
  width: 70px;
  height: 70px;
  position: relative;
  float: left;
  background: linear-gradient(120deg, #e5e5e5 30%, #f0f0f0 38%, #f0f0f0
  40%, #e5e5e5 48%);
  border-radius: 50%;
  background-size: 200% 100%;
  background-position: 100% 0;
  animation: load89234 2s infinite;
}

.card_load_extreme_title {
  width: 90px;
  height: 10px;
  position: relative;
  float: right;
  border-radius: 5px;
  background: linear-gradient(120deg, #e5e5e5 30%, #f0f0f0 38%, #f0f0f0
  40%, #e5e5e5 48%);
  background-size: 200% 100%;
  background-position: 100% 0;
  animation: load89234 2s infinite;
}

.card_load_extreme_descripion {
  width: 90px;
  height: 47px;
  position: relative;
  float: right;
  border-radius: 5px;
  background: linear-gradient(120deg, #e5e5e5 30%, #f0f0f0 38%, #f0f0f0
  40%, #e5e5e5 48%);
  margin-top: 10px;
  background-size: 200% 100%;
  background-position: 100% 0;
  animation: load89234 2s infinite;
}

@keyframes load89234 {
  100% {
    background-position: -100% 0;
  }
}
  • .card设置卡片的宽度、高度、背景色、阴影、位置、变换、内边距和边框半径。
  • .card_load设置主加载条的宽度、高度、背景渐变、边框半径和背景大小。
  • 使用background-position和animation属性创建水平移动的加载效果。
  • .card_load_extreme_title 和 .card_load_extreme_descripion设置标题和描述加载条的宽度、高度、背景渐变、边框半径和背景大小。
  • 同样使用background-position和animation属性创建加载效果。
  • @keyframes load89234定义了一个关键帧动画,使背景位置从100%移动到-100%,实现加载条的动画效果。
相关推荐
c***V3238 小时前
Vue优化
前端·javascript·vue.js
努力往上爬de蜗牛9 小时前
react native真机调试
javascript·react native·react.js
李@十一₂⁰10 小时前
HTML 特殊字体符号
前端·html
y***866910 小时前
TypeScript在Electron应用中的使用
javascript·typescript·electron
zy happy12 小时前
若依 vue3 报错:找不到模块“@/api/xxxx/xxxxx”或其相应的类型声明。。Vue 3 can not find mod
前端·javascript·vue.js
meichaoWen13 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
han_14 小时前
前端高频面试题之CSS篇(一)
前端·css·面试
小小测试开发14 小时前
JMeter XPath2 Extractor用法全解析:精准提取XML/HTML响应数据
xml·jmeter·html
b***748814 小时前
Vue开源
前端·javascript·vue.js
ByteCraze16 小时前
我整理的大文件上传方案设计
前端·javascript