每日CSS_純CSS製作進度條

隨遇丿而安發表於2020-12-26

每日CSS_純CSS製作進度條

2020_12_26

進度條

原始碼


1. 程式碼解析

1.1 html 程式碼解析

  • 設定整個容器
<div class="container">
  ...
</div>
  • 設定主題和第一行進度條, 主題用 <h2> 標識, 每個進度條用 <skills> 標識, 如圖所示
<div class="container">
  <h2>前端技能</h2>
  <div class="skills">
    <span class="Name">Html</span>
    <div class="percent">
      <div class="progress" ></div>
    </div>
    <span class="Value">95%</span>
  </div>
    ...
</div>

第一個

  • 設定其他的行, 與第一行類似, 原理也一樣
<div class="skills">
  <span class="Name">CSS</span>
  <div class="percent">
    <div class="progress" ></div>
  </div>
  <span class="Value">90%</span>
</div>
<div class="skills">
  <span class="Name">JavaScript</span>
  <div class="percent">
    <div class="progress"></div>
  </div>
  <span class="Value">72%</span>
</div>
<div class="skills">
  <span class="Name">frame</span>
  <div class="percent">
    <div class="progress"></div>
  </div>
  <span class="Value">50%</span>
</div>

1.2 css 程式碼解析

  • 初始化所有邊距, 設定尺寸計算方式為 border-box, 設定 body 佈局方式, 將內容居中顯示, 設定背景和字型大小
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body{
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #2e2e2e;
  font-size: 16px;
}
  • 設定 h2 標籤的顏色
.container h2{
  color: #fff;
}
  • 設定每行的格式, 上下邊距, 左右邊距, 設定過渡效果時間 0.5s .

    margin 和 padding 有一個值, 有兩個值, 有三個值, 有四個值時賦予四個方向值的方式, 1個值, 上下左右這個值; 2個值, 分別表示上下和左右; 3個值, 分別表示上, 右, 下, 剩下一個左和右儲存一致; 4個值, 分別表示, 上, 右, 下, 左

.container .skills{
  position: relative;
  display: flex;
  /* 只有兩個數值, 分別表示上下和左右, 上下 20px, 左右 0 */ 
  margin: 20px 0;
  /* 上右下, 缺少一個左, 左和右保持一致, 左的內邊距為 10px */
  padding: 24px 10px 18px;
  background: linear-gradient(#616161 0%, #333 10%, #222);;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid #000;
  transition: 0.5s;
}
  • 設定過渡效果, 移入容器時, 透明度設為0.05, 並將選中的那行放大, 效果如下
.container:hover .skills{
  opacity: 0.05;
}
.container .skills:hover{
  opacity: 1;
  transform: scale(1.1);
}

原始樣式

移入

  • 設定每一行的分層效果, 分為上下兩層
.container .skills:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50%;
  background: rgba(255, 255, 255, .1);
  z-index: 10;
}
  • 設定一行中, name, value, 和 progress 的效果, 其中的動畫只設定了一個 from, to 狀態由最後的 width 屬性決定.
.container .skills .Name{
  position: relative;
  width: 120px;
  text-align: right;
  color: #fff;
  margin-top: -2px;
  text-transform: uppercase;
}
.container .skills .Value{
  position: relative;
  width: 40px;
  text-align: right;
  color: #fff;
  margin-top: -2px;
  text-transform: uppercase;
}
.container .skills .percent{
  position: relative;
  width: 200px;
  height: 16px;
  margin: 0 10px;
  border-radius: 10px;
  background: #151515;
  box-shadow: inset 0 0 10px #000;
  overflow: hidden;
}
.container .skills .percent .progress{
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 95%;
  border-radius: 10px;
  background: #fff;
  box-shadow: inset 0 0 2px #000;
  animation: animate 4s ease-in-out forwards;
}
@keyframes animate {
  from{
    width: 0;
  }
}
  • 設定各個進度條的顏色, 寬度值
.container .skills:nth-child(2) .percent .progress{
  background: linear-gradient(45deg, #1fe6ff, #673AB7);
  width: 95%;
}
.container .skills:nth-child(3) .percent .progress{
  background: linear-gradient(45deg, #3bc0ff, #33ff00);
  width: 90%;
}
.container .skills:nth-child(4) .percent .progress{
  background: linear-gradient(45deg, #ffee54, #ff00ca);
  width: 72%;
}
.container .skills:nth-child(5) .percent .progress{
  background: linear-gradient(45deg, #ffee54, #ff00ca);
  width: 50%;
}

2. 原始碼

2.1 html 原始碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="2020_12_26.css">
</head>
<body>
<div class="container">
  <h2>前端技能</h2>
  <div class="skills">
    <span class="Name">Html</span>
    <div class="percent">
      <div class="progress" ></div>
    </div>
    <span class="Value">95%</span>
  </div>
  <div class="skills">
    <span class="Name">CSS</span>
    <div class="percent">
      <div class="progress" ></div>
    </div>
    <span class="Value">90%</span>
  </div>
  <div class="skills">
    <span class="Name">JavaScript</span>
    <div class="percent">
      <div class="progress"></div>
    </div>
    <span class="Value">72%</span>
  </div>
  <div class="skills">
    <span class="Name">frame</span>
    <div class="percent">
      <div class="progress"></div>
    </div>
    <span class="Value">50%</span>
  </div>

</div>
</body>
</html>

2.2 css 原始碼

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body{
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #2e2e2e;
  font-size: 16px;
}
.container h2{
  color: #fff;
}
.container .skills{
  position: relative;
  display: flex;
  margin: 20px 0;
  padding: 24px 10px 18px;
  background: linear-gradient(#616161 0%, #333 10%, #222);;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid #000;
  transition: 0.5s;
}
.container:hover .skills{
  opacity: 0.05;
}
.container .skills:hover{
  opacity: 1;
  transform: scale(1.1);
}
.container .skills:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50%;
  background: rgba(255, 255, 255, .1);
  z-index: 10;
}
.container .skills .Name{
  position: relative;
  width: 120px;
  text-align: right;
  color: #fff;
  margin-top: -2px;
  text-transform: uppercase;
}
.container .skills .Value{
  position: relative;
  width: 40px;
  text-align: right;
  color: #fff;
  margin-top: -2px;
  text-transform: uppercase;
}
.container .skills .percent{
  position: relative;
  width: 200px;
  height: 16px;
  margin: 0 10px;
  border-radius: 10px;
  background: #151515;
  box-shadow: inset 0 0 10px #000;
  overflow: hidden;
}
.container .skills .percent .progress{
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 95%;
  border-radius: 10px;
  background: #fff;
  box-shadow: inset 0 0 2px #000;
  animation: animate 4s ease-in-out forwards;
}
@keyframes animate {
  from{
    width: 0;
  }
}
.container .skills:nth-child(2) .percent .progress{
  background: linear-gradient(45deg, #1fe6ff, #673AB7);
  width: 95%;
}
.container .skills:nth-child(3) .percent .progress{
  background: linear-gradient(45deg, #3bc0ff, #33ff00);
  width: 90%;
}
.container .skills:nth-child(4) .percent .progress{
  background: linear-gradient(45deg, #ffee54, #ff00ca);
  width: 72%;
}
.container .skills:nth-child(5) .percent .progress{
  background: linear-gradient(45deg, #ffee54, #ff00ca);
  width: 50%;
}

相關文章