स्क्रोल पोज़िशन अवेयर एलिमेंट

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

खास जानकारी

फ़्लोटिंग बटन, जैसे कि "सबसे ऊपर वापस जाएं" लिंक को सिर्फ़ तब दिखाएं, जब वे काम के हों. इससे उपयोगकर्ता अनुभव को बेहतर बनाया जा सकता है. इस गाइड में, सीएसएस container-scroll-state-queries का इस्तेमाल करके इन एलिमेंट को बनाने का तरीका बताया गया है. इससे एलिमेंट को उनके कंटेनर की स्क्रोल पोज़िशन के आधार पर स्टाइल किया जा सकता है. इसके लिए, JavaScript स्क्रोल लिसनर या ऑब्ज़र्वर पर निर्भर रहने की ज़रूरत नहीं पड़ती.

लागू करना

1. स्क्रोल कंटेनर तय करना

स्क्रोल कंटेनर को स्क्रोल-स्टेट क्वेरी कंटेनर के तौर पर एलान करना ज़रूरी है.

.scroller {
  overflow-y: auto;
  /* Establish this element as a scroll-state query container */
  container-type: scroll-state;
}

2. फ़्लोटिंग एलिमेंट को स्टाइल करना

एलिमेंट को कंटेनर के अंदर रखें और उसे स्टाइल करें. डिफ़ॉल्ट रूप से, इसे छिपाया जाना चाहिए.

.back-to-top {
  position: sticky;
  bottom: 20px;
  visibility: hidden;
  opacity: 0;
  translate: 0 20px;
  transition:
    visibility 0.3s,
    opacity 0.3s ease,
    translate 0.3s ease;
}

3. स्क्रोल की स्थिति के हिसाब से जवाब देना

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

/* When the container can be scrolled toward the top, it means the user has scrolled down */
@container scroll-state(scrollable: top) {
  .back-to-top {
    visibility: visible;
    opacity: 1;
    translate: 0 0;
  }
}

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

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

अगर container-scroll-state-queries काम नहीं करता है, तो डिफ़ॉल्ट visibility: hidden की वजह से फ़्लोटिंग एलिमेंट नहीं दिखेगा. यह पक्का करने के लिए कि एलिमेंट काम करे, आपके पास यह चुनने का विकल्प होता है कि वह एलिमेंट, काम न करने वाले ब्राउज़र में हमेशा दिखे.

/* Fallback for browsers that do not support the feature */
.back-to-top {
  visibility: visible; /* Always visible */
  opacity: 1;
}

/* Override for supported browsers to handle dynamic visibility */
@supports (container-type: scroll-state) {
  .back-to-top {
    visibility: hidden;
    opacity: 0;
  }
  
  @container scroll-state(scrollable: top) {
    .back-to-top {
      visibility: visible;
      opacity: 1;
      translate: 0 0;
    }
  }
}

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

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

<!-- Sentinel element placed at the top of the scroller -->
<div class="scroll-sentinel"></div>
/* Marker styling to ensure it does not affect layout */
.scroll-sentinel {
  height: 0;
  width: 0;
  visibility: hidden;
}

.scrolled .back-to-top {
  visibility: visible;
  opacity: 1;
  translate: 0 0;
}
if (!CSS.supports('container-type', 'scroll-state')) {
  const sentinel = document.querySelector('.scroll-sentinel');
  const scroller = document.querySelector('.scroller');

  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      // If the sentinel is NOT intersecting, it means the user has scrolled down
      if (!entry.isIntersecting) {
        scroller.classList.add('scrolled');
      } else {
        scroller.classList.remove('scrolled');
      }
    });
  }, { root: scroller });

  observer.observe(sentinel);
}