عناصر آگاه از موقعیت پیمایش

نمای کلی

تجربه کاربری دکمه‌های شناور، مانند پیوند «بازگشت به بالا»، را با نشان دادن آن‌ها فقط در مواقعی که مفید هستند بهبود بخشید. این راهنما نشان می‌دهد که چگونه این عناصر را بااستفاده از CSS container-scroll-state-queries بسازید، که امکان سبک‌دهی به عناصر را براساس موقعیت پیمایش محفظه آن‌ها بدون تکیه بر شنوندگان یا ناظران پیمایش جاوا اسکریپت فراهم می‌کند.

پیاده‌سازی

۱. ایجاد محتوی پیمایش

محتوای پیمایش باید به‌عنوان محتوای پُرسمان حالت پیمایش اعلام شود.

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

۲. سبک‌بندی عنصر شناور

عنصر را داخل محتوی قرار دهید و به آن سبک بدهید. به‌طور پیش‌فرض، باید پنهان باشد.

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

۳. پاسخ به «وضعیت پیمایش»

از قانون @container با تابع scroll-state استفاده کنید. برای بررسی اینکه آیا کاربر به پایین پیمایش کرده است، بررسی کنید که آیا محفظه به بالا قابل پیمایش است یا نه.

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