এক নজরে
"উপরে ফিরে যান" লিঙ্কের মতো ফ্লোটিং বোতামের ব্যবহারকারীর অভিজ্ঞতা উন্নত করুন, শুধুমাত্র যখন সেগুলি উপযোগী হয় তখন সেগুলি দেখান। এই নির্দেশিকা দেখায় যে কীভাবে 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;
}
৩. স্ক্রল স্টেট অনুযায়ী কাজ করা
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);
}