Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

วิธีทำ Animation ให้ Bullet Points ใน List ด้วย CSS

การเพิ่ม Animation ให้ Bullet Points ทำให้ List น่าสนใจและโต้ตอบได้มากขึ้น ตัวอย่างนี้แสดง 2 แบบ: Unordered List กับ Hover Effect และ Ordered List กับ Counter Animation

Syntax

li {
  list-style: /* value */;
  transition: /* properties */;
}
li:hover {
  /* hover styles */
}

ตัวอย่างที่ 1: Unordered List - Hover Animation

<!DOCTYPE html>
<html>
<head>
  <style>
    li {
      margin: 3px 0;
      padding: 2%;
      width: 28%;
      line-height: 1.2%;
      list-style: none;
      border-radius: 5% 0 0 5%;
      box-shadow: -10px 2px 4px 0 chartreuse;
      color: cornflowerblue;
      transition: all 0.3s ease;
    }
    li:hover {
      box-shadow: -10px 2px 4px 0 blue !important;
      font-size: 1.4em;
      transform: translateX(10px);
    }
  </style>
</head>
<body>
  <ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    <li>d</li>
  </ul>
</body>
</html>

เทคนิคที่ใช้

  • list-style: none - ซ่อน Bullet เริ่มต้น
  • box-shadow - สร้าง "Bullet" แบบ Custom ด้วยเงา
  • transition: all 0.3s ease - ทำให้การเปลี่ยนแปลงนุ่มนวล
  • transform: translateX(10px) - ขยับ Item ไปขวาเมื่อ Hover

ตัวอย่างที่ 2: Ordered List - Counter + Hover

<!DOCTYPE html>
<html>
<head>
  <style>
    ol {
      list-style: none;
      counter-reset: li;
      overflow: hidden;
    }
    li {
      margin-right: 10%;
      padding: 2%;
      width: 15%;
      float: left;
      line-height: 1.2%;
      font-weight: bold;
      counter-increment: li;
      box-shadow: inset 2px 14px 10px lightblue;
      transition: all 0.3s ease;
    }
    li:hover {
      box-shadow: inset 6px 14px 10px lightgreen !important;
      font-size: 1.4em;
      transform: scale(1.1);
    }
    li::before {
      content: counter(li);
      color: seagreen;
      display: inline-block;
      width: 40%;
      margin-left: -2em;
    }
  </style>
</head>
<body>
  <ol>
    <li>a</li>
    <li>b</li>
    <li>c</li>
  </ol>
</body>
</html>

เทคนิคที่ใช้

  • counter-reset/increment - สร้างตัวเลขลำดับเอง
  • li::before - แสดงตัวเลขเป็น "Custom Bullet"
  • float: left - เรียงแนวนอน (ในจริงควรใช้ Flexbox/Grid)
  • Hover: เปลี่ยน Box-shadow, Scale ขึ้น, Font-size ใหญ่ขึ้น

Modern Animation Patterns

1. Staggered Entrance Animation

@keyframes slideIn {
  from { opacity: 0; transform: translateX(-20px); }
  to { opacity: 1; transform: translateX(0); }
}
li {
  animation: slideIn 0.4s ease forwards;
  opacity: 0;
}
li:nth-child(1) { animation-delay: 0.1s; }
li:nth-child(2) { animation-delay: 0.2s; }
li:nth-child(3) { animation-delay: 0.3s; }
/* หรือใช้ CSS: animation-delay: calc(var(--i) * 0.1s); */

2. Bullet Morphing Animation

li::marker {
  content: "○";
  transition: all 0.3s ease;
}
li:hover::marker {
  content: "●";
  color: #e91e63;
  transform: scale(1.5);
}

3. Progress Bar List Items

.progress-item {
  position: relative;
  padding-left: 2rem;
}
.progress-item::before {
  content: "";
  position: absolute;
  left: 0; top: 50%;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: #ddd;
  transform: translateY(-50%);
  transition: all 0.3s;
}
.progress-item.completed::before {
  background: #4caf50;
  box-shadow: 0 0 0 3px rgba(76,175,80,0.3);
}
.progress-item.current::before {
  animation: pulse 1.5s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(76,175,80,0.4); }
  50% { box-shadow: 0 0 0 8px rgba(76,175,80,0); }
}

4. Checkbox-style Interactive List

.check-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem;
  cursor: pointer;
  transition: background 0.2s;
}
.check-list li:hover { background: #f5f5f5; }
.check-list input[type="checkbox"] {
  appearance: none;
  width: 20px; height: 20px;
  border: 2px solid #999;
  border-radius: 4px;
  transition: all 0.2s;
}
.check-list input:checked {
  background: #4caf50;
  border-color: #4caf50;
}
.check-list input:checked::after {
  content: "✓";
  display: block;
  text-align: center;
  color: white;
  font-size: 14px;
  animation: pop 0.2s ease;
}
@keyframes pop {
  0% { transform: scale(0); }
  50% { transform: scale(1.3); }
  100% { transform: scale(1); }
}

Performance Tips

  • Animate แค่ transform และ opacity (GPU Accelerated)
  • หลีกเลี่ยง Animate width, height, margin, font-size (Trigger Layout Reflow)
  • ใช้ will-change: transform สำหรับ Item ที่ Animate บ่อย
  • Respect prefers-reduced-motion สำหรับ Accessibility
@media (prefers-reduced-motion: reduce) {
  li, li::marker, li::before {
    animation: none !important;
    transition: none !important;
  }
}