ภาพรวม
คำแนะนำแบบภาพ เช่น เงาหรือการไล่ระดับสี ช่วยให้ผู้ใช้เข้าใจว่าสามารถเลื่อนเพื่อดูเนื้อหาเพิ่มเติมได้ คู่มือนี้จะแสดงวิธีสร้างคำแนะนำเหล่านี้โดยใช้ CSS container-scroll-state-queries ซึ่งช่วยให้จัดสไตล์องค์ประกอบตามสถานะที่เลื่อนได้ของคอนเทนเนอร์โดยไม่ต้องอาศัย Listener หรือ Observer การเลื่อนของ JavaScript
การใช้งาน
1. สร้างคอนเทนเนอร์การเลื่อน
ต้องประกาศคอนเทนเนอร์การเลื่อนเป็นคอนเทนเนอร์การค้นหาสถานะการเลื่อน
.scroller {
overflow-y: auto;
/* Establish this element as a scroll-state query container */
container-type: scroll-state;
position: relative;
}
2. จัดรูปแบบตัวบ่งชี้
วางองค์ประกอบตัวบ่งชี้ (เช่น เงา การไล่ระดับสี หรือลูกศร) ไว้ในคอนเทนเนอร์และจัดรูปแบบ โดยค่าเริ่มต้น ผู้ใช้ไม่ควรเห็นข้อมูลนี้ เมื่อแสดงโฆษณา โฆษณาไม่ควรเป็นแบบอินเทอร์แอกทีฟ โดยตั้งค่า pointer-events: none
.indicator-top, .indicator-bottom {
position: sticky;
left: 0;
right: 0;
height: 20px;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none; /* Let clicks pass through */
}
.indicator-top {
top: 0;
background: linear-gradient(to bottom, rgba(0,0,0,0.2), transparent); /* Example: Shadow */
}
.indicator-bottom {
bottom: 0;
background: linear-gradient(to top, rgba(0,0,0,0.2), transparent); /* Example: Shadow */
}
3. ค้นหาสถานะการเลื่อน
ใช้กฎ @container กับฟังก์ชัน scroll-state ตรวจสอบว่าคอนเทนเนอร์เลื่อนขึ้นหรือลงได้เพื่อแสดงตัวบ่งชี้ที่เกี่ยวข้อง
/* Show top indicator when the user can scroll up */
@container scroll-state(scrollable: top) {
.indicator-top {
opacity: 1;
}
}
/* Show bottom indicator when the user can scroll down */
@container scroll-state(scrollable: bottom) {
.indicator-bottom {
opacity: 1;
}
}
กลยุทธ์สำรอง
การทำงานสำรองพื้นฐาน
หากไม่รองรับฟีเจอร์นี้ ตัวบ่งชี้จะยังคงมองไม่เห็น เนื่องจากข้อมูลเหล่านี้เป็นคำแนะนำและไม่ได้มีความสำคัญต่อฟังก์ชันการทำงาน คุณจึงละเว้นข้อมูลเหล่านี้ในเบราว์เซอร์ที่ไม่รองรับได้
โฆษณาสำรองขั้นสูง (Intersection Observer)
หากจำเป็นต้องใช้คำแนะนำ ให้ใช้ IntersectionObserver เพื่อสลับคลาสเมื่อองค์ประกอบ Sentinel ที่ด้านบนและด้านล่างของแถบเลื่อนย้ายเข้าและออกจากพอร์ตการเลื่อน
<!-- Sentinel elements placed at the ends of the scroller -->
<div class="sentinel-top"></div>
<!-- Content goes here -->
<div class="sentinel-bottom"></div>
/* Marker styling to ensure it does not affect layout */
.sentinel-top, .sentinel-bottom {
height: 0;
width: 0;
visibility: hidden;
}
.scroller.scrolled-down .indicator-top {
opacity: 1;
}
.scroller.can-scroll-down .indicator-bottom {
opacity: 1;
}
if (!CSS.supports('container-type', 'scroll-state')) {
const topSentinel = document.querySelector('.sentinel-top');
const bottomSentinel = document.querySelector('.sentinel-bottom');
const scroller = document.querySelector('.scroller');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.target === topSentinel) {
// If top sentinel is not intersecting, we have scrolled down
scroller.classList.toggle('scrolled-down', !entry.isIntersecting);
}
if (entry.target === bottomSentinel) {
// If bottom sentinel is intersecting, we reached the bottom
scroller.classList.toggle('can-scroll-down', !entry.isIntersecting);
}
});
}, { root: scroller });
observer.observe(topSentinel);
observer.observe(bottomSentinel);
}