স্ক্রল করার সুবিধা সংক্রান্ত ইঙ্গিত

এক নজরে

ছায়া বা গ্রেডিয়েন্টের মতো ভিজ্যুয়াল ইঙ্গিত ব্যবহারকারীদের বুঝতে সাহায্য করে যে তারা আরও কন্টেন্ট দেখতে স্ক্রল করতে পারবেন। এই নির্দেশিকা দেখায় যে কীভাবে CSS container-scroll-state-queries ব্যবহার করে এইসব ইঙ্গিত তৈরি করতে হয়, যা JavaScript স্ক্রল লিসেনার বা অবজারভারের উপর নির্ভর না করে কন্টেনারের স্ক্রলযোগ্য অবস্থার উপর ভিত্তি করে স্টাইলিং এলিমেন্টকে অনুমতি দেয়।

প্রয়োগ

১. স্ক্রল কন্টেনার তৈরি করুন

স্ক্রল কন্টেনারকে স্ক্রল-স্টেট কোয়েরি কন্টেনার হিসেবে ঘোষণা করতে হবে।

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

২. ইন্ডিকেটর স্টাইল করুন

কন্টেনারের মধ্যে ইন্ডিকেটর এলিমেন্ট (যেমন, শ্যাডো, গ্রেডিয়েন্ট বা তীরচিহ্ন) রাখুন এবং সেগুলিকে স্টাইল করুন। ডিফল্ট হিসেবে, এগুলি দেখা যাবে না। এগুলি দেখানোর সময়, 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 */
}

৩. স্ক্রল স্টেট কোয়েরি করুন

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