स्क्रोल करने की सुविधा के बारे में बताने वाले संकेत

वेब से जुड़ी सुविधाएं: कंटेनर स्क्रोल-स्टेट क्वेरी

खास जानकारी

शैडो या ग्रेडिएंट जैसे विज़ुअल हिंट से, उपयोगकर्ताओं को यह समझने में मदद मिलती है कि वे ज़्यादा कॉन्टेंट देखने के लिए स्क्रोल कर सकते हैं. इस गाइड में, सीएसएस container-scroll-state-queries का इस्तेमाल करके इन हिंट को बनाने का तरीका बताया गया है. इससे कंटेनर के स्क्रोल किए जा सकने वाले स्टेटस के आधार पर, एलिमेंट को स्टाइल किया जा सकता है. इसके लिए, 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. स्क्रोल की स्थिति के बारे में क्वेरी करना

scroll-state फ़ंक्शन के साथ @container नियम का इस्तेमाल करें. देखें कि कंटेनर को ऊपर या नीचे स्क्रोल किया जा सकता है या नहीं, ताकि उससे जुड़ा इंडिकेटर दिखाया जा सके.

/* 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;
  }
}

फ़ॉलबैक रणनीतियां

बेसिक फ़ॉलबैक

अगर यह सुविधा काम नहीं करती है, तो इंडिकेटर नहीं दिखेंगे. ये सिर्फ़ हिंट हैं और फ़ंक्शन के लिए ज़रूरी नहीं हैं. इसलिए, जिन ब्राउज़र पर ये काम नहीं करते उनमें इन्हें शामिल न करना ठीक है.

ऐडवांस फ़ॉलबैक (इंटरसेक्शन ऑब्ज़र्वर)

अगर हिंट ज़रूरी हैं, तो स्क्रोलर के सबसे ऊपर और सबसे नीचे मौजूद सेंटिनल एलिमेंट के स्क्रोलपोर्ट में अंदर और बाहर जाने पर, क्लास को टॉगल करने के लिए IntersectionObserver का इस्तेमाल करें.

<!-- 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);
}