使

使用 HTML 和 CSS 构建一个简单的卡片骨架加载器组件。步骤 1 - HTML 步骤 2 - 添加卡片和骨架的通用样式。步骤 3 - 添加卡片特定样式。步骤 4 - 添加骨架特定样式。步骤 5 - 添加动画

2025-05-24

使用 HTML 和 CSS 构建一个简单的卡片骨架加载器组件。

步骤 1.- HTML

第 2 步 - 添加卡片和骨架的通用样式。

步骤 3——添加卡片特定的样式。

步骤 4 - 添加骨架特定样式

步骤 5 - 添加动画

大家好!在本教程中,我们将使用 HTML 和 CSS 构建一个简单的骨架加载器组件。您可以在您的网站/应用中使用此组件作为主内容加载前的后备选项。

替代文本

步骤 1.- HTML

我们将创建一张卡片及其骨架加载器,因此让我们首先添加卡片和骨架的 HTML。

卡片的 HTML

<div class="card">
          <div>
              <img class="card-img" src='https://avataaars.io/?avatarStyle=Transparent&topType=ShortHairShortWaved&accessoriesType=Prescription01&hairColor=Black&facialHairType=Blank&clotheType=Hoodie&clotheColor=Blue03&eyeType=Default&eyebrowType=Default&mouthType=Twinkle&skinColor=Light'/>
          </div>
          <div class="card-text">
              <h2 class="card-title">Gaurav Gawade</h2>
              <p class="card-description">
                 <span>Lorem ipsum dolor sit amet consectetur, adipisicing eli.</span> 
              </p>
              <ul class="card-skills">
                  <li class="skill">UI Designer.</li>
                  <li class="skill">UX Designer.</li>
                  <li class="skill">Web Developer.</li>
              </ul>
              <a href="#" class="card-link">Read More</a>
          </div>
 </div>
Enter fullscreen mode Exit fullscreen mode

HTML 框架

(我们将保持卡片的基本结构相同,但我们将删除内容。此外,更改元素上的类)

<div class="skeleton">
          <div class="skeleton-img"></div>
          <div class="skeleton-text">
              <h2 class="skeleton-title"></h2>
              <p class="skeleton-description">
                <span></span>
                <span></span>
              </p>
              <ul class="skeleton-skills">
                  <li class="skill"></li>
                  <li class="skill"></li>
                  <li class="skill"></li>
              </ul>
              <a href="#" class="skeleton-link"></a>
          </div>
</div>
Enter fullscreen mode Exit fullscreen mode

第 2 步 - 添加卡片和骨架的通用样式。

(为了保持一致的外观)

    .card,
    .skeleton {
      display: flex;
      justify-content: space-around;
      padding: 20px;
      max-width: 400px;
      height: 155px;
      margin: 20px;
      border-radius: 10px;
      background-color: #E2E8F0;
      box-shadow:
        0 9px 33px rgba(0, 0, 0, 0.07);
    }


    .card-text, .skeleton-text{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        width: 250px;
        margin-left: 10px;
    }


    .card-img, .skeleton-img{
        width: 75px;
        height: 75px;
        border-radius: 50%;
    }


    .card-description, .skeleton-description{
        margin: 10px 0;
    }


    .card-link, .skeleton-link{
        margin-top: 20px;
    }


    .card-skills , .skeleton-skills{
        display: flex;
        justify-content: space-between;
    }
Enter fullscreen mode Exit fullscreen mode

步骤 3——添加卡片特定的样式。

(字体大小、颜色等)

    .card-img{
        background-color: #4F46E5;
    }

    .card-title{
        font-size: 16px;
        color: #334155;
    }

    .card-description{
        font-size: 12px;
        color: #64748B;
    }

    .card-skills .skill {
        font-size: 12px;
        font-weight: 600;
        color: #475569;
    }

    .card-link{
        font-size: 12px;
        color: #4F46E5;
    }
Enter fullscreen mode Exit fullscreen mode

步骤 4 - 添加骨架特定样式

(在这里,我们将指定骨架的高度、宽度和背景颜色。)

.skeleton-img{
        animation: pulse 2s infinite ease-in-out;
    }

    .skeleton-description span:nth-child(1){
        display: block;
        width: 210px;
        height: 10px;
        background-color: #94A3B8;
    }

    .skeleton-description span:nth-child(2){
        display: block;
        width: 150px;
        height: 10px;
        background-color: #94A3B8;
        margin-top: 5px;
    }

    .skeleton-skills .skill{
        width: 65px;
        height: 12px;
        background-color: #94A3B8;
    }

    .skeleton-link{
        width: 75px;
        height: 12px;
        background-color: #94A3B8;
    }
Enter fullscreen mode Exit fullscreen mode

现在卡片和骨架应该看起来像这样

替代文本

快完成了!现在,我们来给骨骼添加一些精细的动画。

步骤 5 - 添加动画

    @keyframes pulse {
        0% {
            background-color: #94A3B8;
        }

        50% {
            background-color: #CBD5E1;
        }

        100% {
            background-color: #94A3B8;
        }
    }
Enter fullscreen mode Exit fullscreen mode

现在我们可以在所有骨架上添加此动画并删除背景颜色。

// replace all styles from skeletons with these styles

    .skeleton-title{
        width: 150px;
        height: 12px;
        animation: pulse 2s infinite ease-in-out;
    }

    .skeleton-img{
        animation: pulse 2s infinite ease-in-out;
    }

    .skeleton-description span:nth-child(1){
        display: block;
        width: 210px;
        height: 10px;
        animation: pulse 2s infinite ease-in-out;
    }

    .skeleton-description span:nth-child(2){
        display: block;
        width: 150px;
        height: 10px;
        animation: pulse 2s infinite ease-in-out;
        margin-top: 5px;
    }

    .skeleton-skills .skill{
        width: 65px;
        height: 12px;
        animation: pulse 2s infinite ease-in-out;
    }

    .skeleton-link{
        width: 75px;
        height: 12px;
        animation: pulse 2s infinite ease-in-out;
    }
Enter fullscreen mode Exit fullscreen mode

最终演示

替代文本

希望这篇文章能帮到你。我写得不好,代码解释得不好,请见谅😅
非常感谢 🙂

文章来源:https://dev.to/devggaurav/build-a-simple-card-sculpture-loader-component-using-html-and-css-3a20
PREV
让我们使用 HTML、CSS 和 JavaScript 构建一个响应式导航栏和汉堡菜单。首先从 HTML 开始,然后添加 CSS 重置,最后逐个为元素添加样式。
NEXT
6 amazing free tools that will save you some time when u are building websites. (Especially for 'non-designer' developers) 1. Type Scale 2. Happy Hues 3. Fontjoy 4. CSS Peeper 5. avataaars generator 6. brumm.af