ในอดีต การใช้สไตล์ที่ไม่ซ้ำกันกับพี่น้องแต่ละรายการในลิสต์ต้องใช้ลูป :nth-child ที่ซับซ้อนหรือ JavaScript เพื่อแทรกสไตล์ในบรรทัด CSS สมัยใหม่มี sibling-index() และ sibling-count() เพื่อทำการคำนวณเหล่านี้ในสไตล์ชีตโดยตรง ซึ่งช่วยให้เลย์เอาต์และระบบสีแบบไดนามิกปรับเปลี่ยนโดยอัตโนมัติเมื่อมีการเพิ่มหรือนำองค์ประกอบออก
ระบบสีแบบไดนามิก
คุณสามารถสร้างสเปกตรัมสีในกลุ่มพี่น้องได้โดยการคำนวณค่าสีหรือความสว่างที่ไม่ซ้ำกันสำหรับองค์ประกอบย่อยแต่ละรายการ ซึ่งจะช่วยให้มั่นใจได้ว่าเอฟเฟกต์การไล่ระดับสีจะสอดคล้องกันไม่ว่าจะมีรายการกี่รายการก็ตาม
.swatch {
/* Calculate hue by dividing the full 360deg circle by total siblings */
/* and multiplying by the current element's 1-based index */
background-color: hsl(
calc(360deg / sibling-count() * sibling-index()),
70%,
50%
);
}
เลย์เอาต์แบบสมมาตรและเอฟเฟกต์พัดลม
หากต้องการสร้างเอฟเฟกต์แบบสมมาตร (เช่น "พัด" หรือการจัดรายการให้อยู่ตรงกลาง) ให้ใช้จำนวนทั้งหมดเพื่อหาจุดกึ่งกลางของรายการ
.card {
/* Find the center index (e.g., 3 if there are 5 siblings) */
--center: calc((sibling-count() + 1) / 2);
/* Rotate items away from the center: negative for left, positive for right */
/* center element gets 0deg rotation */
transform: rotate(calc(10deg * (sibling-index() - var(--center))));
}
การจัดตำแหน่งแบบวงกลมและซับซ้อน
เมื่อรวมฟังก์ชันเหล่านี้เข้ากับตรีโกณมิติ CSS (sin(), cos()) คุณจะวางองค์ประกอบในวงกลมที่สมบูรณ์แบบได้โดยไม่ต้องใช้พิกัดด้วยตนเอง
.orb {
/* Calculate the angle for this item's position on a 360deg circle */
--angle: calc(360deg / sibling-count() * sibling-index());
--radius: 150px;
/* Set the pre-transformed position for all items to be centered */
position: absolute;
place-self: center;
/* Position each element around the parent center */
transform: translate(
calc(cos(var(--angle)) * var(--radius)),
calc(sin(var(--angle)) * var(--radius))
);
}
กลยุทธ์สำรอง
หากระบบไม่รองรับ sibling-index() และ sibling-count() ให้ระบุทางเลือกสำรองโดยการแทรกคุณสมบัติที่กำหนดเองของ CSS ผ่าน JavaScript บังคับ: ใช้การตรวจหาฟีเจอร์กับ CSS.supports() เพื่อให้สคริปต์ทำงานเมื่อจำเป็นเท่านั้น
/* MANDATORY: Check for native support before applying fallback */
if (!CSS.supports('top: calc(sibling-index() * 1px)')) {
const items = document.querySelectorAll('.item');
items.forEach((item, index) => {
/* MANDATORY: Injected index must be 1-based to match native function */
item.style.setProperty('--sibling-index', index + 1);
item.style.setProperty('--sibling-count', items.length);
});
}
ใน CSS ให้ใช้ตัวแปรเหล่านี้เป็นฐานและลบล้างด้วยฟังก์ชันเนทีฟภายในบล็อก @supports บังคับ: คุณต้องรวมการลบล้างฟังก์ชันเนทีฟไว้ใน @supports เพื่อให้ตัวแปรยังคงใช้ได้ในเบราว์เซอร์รุ่นเก่า
.item {
/* 1. Set base values using variables (from JS fallback) */
--index: var(--sibling-index);
--count: var(--sibling-count);
/* 2. Use the computed variables - replace this with your implementation-specific styles */
background-color: hsl(calc(360deg / var(--count) * var(--index)), 70%, 50%);
}
@supports (top: calc(sibling-index() * 1px)) {
.item {
/* 3. Override with native functions ONLY if supported */
--index: sibling-index();
--count: sibling-count();
}
}