हेडर टिका रहता है और सिकुड़ता है, और स्क्रॉलर पर लगा scroll-padding-top, जिसे एक ResizeObserver हेडर की ऊँचाई के बराबर रखता है, ब्राउज़र से हर फ़ोकस वाले एलिमेंट को हेडर के नीचे रुकवाता है।एक स्किप लिंक उसे लाँघ जाता है, और स्क्रॉल पर छिपना चालू होने पर, फ़ोकस के हेडर में आते ही हेडर लौट आता है।
इसे माउस, कीबोर्ड या स्क्रीन रीडर से इस्तेमाल करें।साथ का इंस्पेक्टर दिखाता है कि आपके इस्तेमाल के दौरान ब्राउज़र सहायक तकनीक को क्या बताता है: फ़ोकस, स्थिति में बदलाव और घोषणाएँ।
गरम घी-तेल में मसाले खिल उठते हैं। जीरा, राई और सूखी लाल मिर्च लगभग तीस सेकंड में अपना स्वाद छोड़ देते हैं, और घी उसे पूरे बर्तन में फैला देता है। इन्हें पहले डाल दें तो स्वाद पककर उड़ जाता है।
घी मक्खन से ज़्यादा आँच बिना जले सह लेता है। घर पर घी बनाने की हमारी गाइड में बीस मिनट और मक्खन का एक पैकेट लगता है।
थाली की हर रेसिपी छपने से पहले चेन्नई की एक घरेलू रसोई में पकाई जाती है।
डेमो साइट को स्क्रॉल करें, या उसमें Tab दबाते हुए आगे बढ़ें।
हेडर की ऊँचाई
0 px
स्क्रॉल पैडिंग
0 px
जावास्क्रिप्ट के बिना डेमो उतना ही काम करता है जितना उसका एचटीएमएल; इंस्पेक्टर के लिए जावास्क्रिप्ट चाहिए।
कीबोर्ड
जिन कुंजियों पर यह काम करता है
हर क्रिया पॉइंटर के बिना काम करती है।
कुंजी
यह क्या करती है
Tab
डेमो साइट पर, फिर स्किप लिंक, हेडर के लिंक और लेख के लिंक पर ले जाता है; जिस पर भी पहुँचता है वह हेडर के नीचे रुकता है, कभी उसके पीछे नहीं।
Enter
“कंटेंट पर जाएँ” पर, फ़ोकस को लेख की शुरुआत पर ले जाता है, जो हेडर के ठीक नीचे तक स्क्रॉल हो जाती है।
Enter
“रेसिपी पर जाएँ” पर, फ़ोकस को हेडर के नीचे रेसिपी की हेडिंग पर ले जाता है; स्क्रॉल तभी स्मूथ होता है जब सिस्टम मोशन की अनुमति देता है।
Shift+Tab
वापस ऊपर ले जाता है; फिर से दिखाई देने वाली जगह में स्क्रॉल हुआ लिंक हेडर के नीचे रुकता है, और हेडर तक पहुँचने पर लेख जहाँ था वहीं रहता है। स्क्रॉल पर छिपना चालू हो तो हेडर लौट आता है।
Arrow DownयाArrow UpयाPage DownयाSpace
डेमो साइट पर फ़ोकस हो तो उसे स्क्रॉल करता है; पहली कुछ पंक्तियों के बाद हेडर सिकुड़ जाता है।
स्क्रीन रीडर
यह क्या घोषित करता है
मार्कअप की भूमिकाओं, नामों और स्थितियों से लिखा गया।
कब
अपेक्षित घोषणा
Tab डेमो साइट तक पहुँचता है
थाली, डेमो साइट, क्षेत्र
अगला Tab स्किप लिंक तक पहुँचता है
कंटेंट पर जाएँ, लिंक
Enter उसे खोलता है
दाल तड़का, हफ़्ते की शाम वाला तरीक़ा, लेख
“रेसिपी पर जाएँ” पर Enter
रेसिपी: चार लोगों के लिए दाल तड़का, हेडिंग स्तर 4
हेडर सिकुड़ता है, खिसककर हटता है या लौट आता है
कुछ नहीं: बदलाव सिर्फ़ दिखने में है, और फ़ोकस जहाँ था वहीं रहता है
ये अपेक्षित घोषणाएँ हैं, रिकॉर्डिंग नहीं।स्क्रीन रीडर और ब्राउज़र के हिसाब से शब्द और क्रम बदलते हैं।
कोड
कोड कॉपी करें
वही फ़ाइलें जिन पर यह डेमो चलता है।स्टाइल एक प्रीफ़िक्स के साथ परोसी जाती हैं जो इस साइट की अपनी स्टाइल को डेमो से दूर रखता है; आप जो कॉपी करते हैं वह मूल है।
मार्कअप
<div class="ap-sticky" data-ap-sticky-header>
<div class="ap-sticky__viewport" role="region" aria-label="Thali, demo site" tabindex="0">
<header class="ap-sticky__header">
<div class="ap-sticky__bar">
<a class="ap-sticky__skip" href="#sticky-article">Skip to content</a>
<a class="ap-sticky__logo" href="#sticky-home">
<svg class="ap-sticky__brand" viewBox="0 0 32 32" aria-hidden="true" focusable="false"><circle class="ap-sticky__plate" cx="16" cy="16" r="15"/><circle cx="10.5" cy="12" r="3.6"/><circle cx="21.5" cy="12" r="3.6"/><circle cx="16" cy="21.5" r="4.6"/></svg>
<span>Thali</span>
</a>
<nav class="ap-sticky__nav" aria-label="Thali">
<ul class="ap-sticky__menu">
<li><a href="#sticky-recipes">Recipes</a></li>
<li><a href="#sticky-techniques">Techniques</a></li>
<li><a href="#sticky-pantry">Pantry</a></li>
</ul>
</nav>
<a class="ap-btn ap-btn--primary ap-sticky__cta" href="#sticky-subscribe">Subscribe</a>
</div>
</header>
<div class="ap-sticky__mark" aria-hidden="true"></div>
<article class="ap-sticky__article" id="sticky-article" tabindex="-1" aria-labelledby="sticky-name">
<p class="ap-sticky__kicker">Weeknight dinners</p>
<h3 class="ap-sticky__name" id="sticky-name">Dal tadka, the weeknight way</h3>
<p class="ap-sticky__byline">Meera Iyer · 6 min read</p>
<div class="ap-sticky__art" aria-hidden="true">
<svg viewBox="0 0 240 120" focusable="false">
<path class="ap-sticky__steam" d="M98 34c-7-7 7-11 0-20M120 30c-7-7 7-11 0-20M142 34c-7-7 7-11 0-20"/>
<ellipse class="ap-sticky__base" cx="120" cy="108" rx="62" ry="6"/>
<path class="ap-sticky__bowl" d="M52 56h136a68 48 0 0 1-136 0z"/>
<ellipse class="ap-sticky__dal-top" cx="120" cy="56" rx="68" ry="11"/>
<ellipse class="ap-sticky__chilli" cx="104" cy="54" rx="8" ry="2.6" transform="rotate(-18 104 54)"/>
<ellipse class="ap-sticky__chilli" cx="141" cy="57" rx="7" ry="2.4" transform="rotate(14 141 57)"/>
<ellipse class="ap-sticky__leaf" cx="122" cy="52" rx="5.5" ry="2.4" transform="rotate(-30 122 52)"/>
<ellipse class="ap-sticky__leaf" cx="88" cy="58" rx="5" ry="2.2" transform="rotate(20 88 58)"/>
<circle class="ap-sticky__seed" cx="114" cy="59" r="1.6"/>
<circle class="ap-sticky__seed" cx="130" cy="54" r="1.6"/>
<circle class="ap-sticky__seed" cx="152" cy="55" r="1.6"/>
<circle class="ap-sticky__seed" cx="96" cy="53" r="1.6"/>
</svg>
</div>
<p class="ap-sticky__lead">Yellow dal, a pressure cooker and a spoon of ghee make dinner in thirty minutes. The tempering goes in last, so it still crackles when it reaches the table.</p>
<a class="ap-sticky__jump" href="#sticky-recipe">
<svg class="ap-sticky__jump-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 5v14M6 13l6 6 6-6"/></svg>
Jump to recipe
</a>
<h4 class="ap-sticky__sub">Why the tadka goes in last</h4>
<p>Spices bloom in hot fat. Cumin, mustard seeds and dried chillies give up their flavour in about thirty seconds, and the ghee carries it through the whole pot. Add them early and the flavour cooks away.</p>
<p>Ghee takes more heat than butter without burning. Our <a href="#sticky-ghee">guide to making ghee at home</a> needs twenty minutes and one block of butter.</p>
<h4 class="ap-sticky__sub">Choose your dal</h4>
<ul class="ap-sticky__dals">
<li class="ap-sticky__dal ap-sticky__dal--toor">
<span class="ap-sticky__dot" aria-hidden="true"></span>
<a href="#sticky-toor">Toor dal</a>
<p class="ap-sticky__note">Split pigeon peas. Creamy, the everyday choice.</p>
</li>
<li class="ap-sticky__dal ap-sticky__dal--masoor">
<span class="ap-sticky__dot" aria-hidden="true"></span>
<a href="#sticky-masoor">Masoor dal</a>
<p class="ap-sticky__note">Red lentils. No soaking, soft in fifteen minutes.</p>
</li>
<li class="ap-sticky__dal ap-sticky__dal--moong">
<span class="ap-sticky__dot" aria-hidden="true"></span>
<a href="#sticky-moong">Moong dal</a>
<p class="ap-sticky__note">Split mung beans. Light and gentle.</p>
</li>
</ul>
<div class="ap-sticky__recipe">
<h4 class="ap-sticky__sub" id="sticky-recipe" tabindex="-1">Recipe: dal tadka for four</h4>
<div class="ap-sticky__cols">
<div>
<h5 class="ap-sticky__colname">Ingredients</h5>
<ul class="ap-sticky__list">
<li>1 cup toor dal, rinsed</li>
<li>3 cups water</li>
<li>Half a teaspoon of turmeric</li>
<li>1 teaspoon salt</li>
<li>2 tablespoons ghee</li>
<li>1 teaspoon cumin seeds</li>
<li>2 dried red chillies</li>
<li>4 cloves of garlic, sliced</li>
</ul>
</div>
<div>
<h5 class="ap-sticky__colname">Steps</h5>
<ol class="ap-sticky__list ap-sticky__list--steps">
<li>Pressure-cook the dal with the water, turmeric and salt for four whistles.</li>
<li>Mash it lightly and simmer for five minutes, adding water until it pours.</li>
<li>Heat the ghee, add the cumin and chillies, then the garlic until golden.</li>
<li>Pour the tadka over the dal and cover it for a minute before serving.</li>
</ol>
</div>
</div>
<p class="ap-sticky__serve">Serve with steamed rice or <a href="#sticky-phulka">soft phulkas</a>.</p>
</div>
<h4 class="ap-sticky__sub">More weeknight dinners</h4>
<ul class="ap-sticky__more">
<li><a href="#sticky-rajma">Rajma in the pressure cooker</a></li>
<li><a href="#sticky-lemon-rice">Lemon rice with peanuts</a></li>
<li><a href="#sticky-palak">Palak paneer without cream</a></li>
</ul>
<p class="ap-sticky__end">Every recipe on Thali is cooked in a home kitchen in Chennai before it is published.</p>
</article>
</div>
<div class="ap-sticky__info">
<p class="ap-sticky__hint">Scroll the demo site, or press Tab through it.</p>
<dl class="ap-sticky__readout" hidden>
<div><dt>Header height</dt><dd><span data-sticky-header-px>0</span> px</dd></div>
<div><dt>Scroll padding</dt><dd><span data-sticky-padding-px>0</span> px</dd></div>
</dl>
</div>
</div>
/**
* Sticky header: a site header that stays at the top of its scroll area,
* shrinks once the reader scrolls, and never covers the element with focus.
*
* Markup: [data-ap-sticky-header] holding a scroll area
* (.ap-sticky__viewport: a labelled region with tabindex=0, and the window
* itself on a real page) whose first child is the sticky
* .ap-sticky__header, with the skip link first inside it. An empty
* .ap-sticky__mark marks how far to scroll before the header shrinks.
* The CSS reads --header-height for scroll-padding-top; this script keeps
* it equal to the header. Add data-hide-on-scroll to the root to slide the
* header away while scrolling down and bring it back on the way up.
*/
const FOCUSABLE = "a[href], button, input, select, textarea, [tabindex]";
const GAP = 12; // the space kept between the header and a focused element
export function init(root) {
const viewport = root.querySelector(".ap-sticky__viewport");
const header = root.querySelector(".ap-sticky__header");
const mark = root.querySelector(".ap-sticky__mark");
const readout = root.querySelector(".ap-sticky__readout");
const headerPx = root.querySelector("[data-sticky-header-px]");
const paddingPx = root.querySelector("[data-sticky-padding-px]");
const reduce = matchMedia("(prefers-reduced-motion: reduce)");
let lastTop = viewport.scrollTop;
let frame = 0;
let landing = null;
let tabFrom = null;
// 1. The header's height decides the scroll padding. It changes when the
// header shrinks, wraps to two rows, or text is enlarged, so measure it.
const measure = new ResizeObserver(() => {
const height = header.offsetHeight;
viewport.style.setProperty("--header-height", `${height}px`);
if (readout) {
headerPx.textContent = String(height);
paddingPx.textContent = String(height + GAP);
readout.hidden = false;
}
});
// The border box: shrinking changes the padding, not the content.
measure.observe(header, { box: "border-box" });
// 2. Shrink once the marker at the top has scrolled out of view: one
// callback per crossing, nothing on every scroll frame.
const watch = new IntersectionObserver(([entry]) => {
header.classList.toggle("ap-sticky__header--compact", !entry.isIntersecting);
}, { root: viewport });
watch.observe(mark);
// 3. Hide on scroll down, show on scroll up, if the option is on. Never
// hidden while something in the header has focus.
function setAway(away) {
header.classList.toggle("ap-sticky__header--away", away && !header.contains(document.activeElement));
}
function onScroll() {
if (frame) return;
frame = requestAnimationFrame(() => {
frame = 0;
const top = viewport.scrollTop;
if (!root.hasAttribute("data-hide-on-scroll") || top <= header.offsetHeight) {
lastTop = top;
setAway(false);
return;
}
// Ignore small wobbles from trackpads; act on a clear direction.
if (Math.abs(top - lastTop) < 6) return;
setAway(top > lastTop);
lastTop = top;
});
}
// 4. A safety net for browsers that ignore scroll-padding when focus
// moves: if the focused element still ends up under the header, scroll it
// clear.
function keepClear(el) {
if (document.activeElement !== el) return;
const away = header.classList.contains("ap-sticky__header--away");
const limit = viewport.getBoundingClientRect().top + viewport.clientTop + (away ? 0 : header.offsetHeight) + GAP;
const top = el.getBoundingClientRect().top;
if (top < limit - 1) viewport.scrollBy({ top: top - limit, behavior: "auto" });
}
// The header's own links sit inside the scroll padding, so a browser
// scrolls the page up to "reveal" one when Tab reaches it, before any
// focus event fires. A sticky header is always in view, so the reader's
// place, noted when Tab was pressed, is put back before the next paint.
function onKeydown(event) {
if (event.key === "Tab") tabFrom = viewport.scrollTop;
}
function onFocusin(event) {
const el = event.target;
if (header.contains(el)) {
setAway(false);
if (tabFrom !== null && viewport.scrollTop !== tabFrom) viewport.scrollTop = tabFrom;
tabFrom = null;
return;
}
tabFrom = null;
if (el === viewport || el === landing) return;
requestAnimationFrame(() => keepClear(el));
}
// 5. Skip and jump links: move focus to the target, placed below the
// header. Moving focus here, rather than following the link, leaves the
// address bar and the Back button alone.
function onClick(event) {
const link = event.target.closest("a[href^='#']");
if (!link || !viewport.contains(link)) return;
const target = document.getElementById(decodeURIComponent(link.hash.slice(1)));
if (!target || !viewport.contains(target)) return;
event.preventDefault();
const padding = parseFloat(getComputedStyle(viewport).scrollPaddingTop) || 0;
const margin = parseFloat(getComputedStyle(target).scrollMarginTop) || 0;
const offset = target.getBoundingClientRect().top - viewport.getBoundingClientRect().top - viewport.clientTop;
let top = viewport.scrollTop + offset - padding - margin;
// Above the marker the header is full height again: show the very top.
if (top < mark.offsetHeight) top = 0;
viewport.scrollTo({ top, behavior: reduce.matches ? "auto" : "smooth" });
if (!target.matches(FOCUSABLE)) target.setAttribute("tabindex", "-1");
landing = target;
target.focus({ preventScroll: true });
landing = null;
}
viewport.addEventListener("scroll", onScroll, { passive: true });
viewport.addEventListener("keydown", onKeydown);
viewport.addEventListener("focusin", onFocusin);
viewport.addEventListener("click", onClick);
return () => {
measure.disconnect();
watch.disconnect();
cancelAnimationFrame(frame);
viewport.removeEventListener("scroll", onScroll);
viewport.removeEventListener("keydown", onKeydown);
viewport.removeEventListener("focusin", onFocusin);
viewport.removeEventListener("click", onClick);
viewport.style.removeProperty("--header-height");
header.classList.remove("ap-sticky__header--compact", "ap-sticky__header--away");
};
}
for (const root of document.querySelectorAll("[data-ap-sticky-header]")) init(root);
स्क्रॉल क्षेत्र पर scroll-padding-top हेडर की मापी गई ऊँचाई और 12 पिक्सेल के बराबर है, इसलिए जब भी Tab या Shift+Tab स्क्रॉल करता है, पूरा फ़ोकस वाला एलिमेंट हेडर के नीचे रुकता है।
जब सिस्टम कम मोशन माँगता है तो सिकुड़ना, खिसकना और स्मूथ जंप रुक जाते हैं: हेडर एक ही क़दम में बदलता है और जंप तुरंत होता है।
उपयोग
इसे कब इस्तेमाल करें
इसे अपनाएँ
ऐसी साइटें जिनके हेडर में सर्च, कार्ट या मुख्य नेविगेशन है, जिसकी ज़रूरत लोगों को लंबे पेज पर कहीं से भी पड़ती है।
पढ़ने के लंबे पेज, जहाँ पतला हेडर ऊपर तक वापस स्क्रॉल किए बिना बाहर निकलने का रास्ता देता है।
कुछ और इस्तेमाल करें
छोटे पेज, जहाँ हेडर वैसे भी दिखाई देता है।
ऐसे हेडर जो फ़ोन की स्क्रीन का लगभग एक चौथाई से ज़्यादा हिस्सा घेर लें: उन्हें पेज के साथ स्क्रॉल होकर हटने दें।
ऐसे पेज जो पहले से स्क्रीन के नीचे एक टूलबार टिकाए रखते हैं: दो टिकी पट्टियाँ पढ़ने के लिए बहुत कम जगह छोड़ती हैं।
आम विफलताएँ
आमतौर पर यह कहाँ गलत होता है
हेडर के नीचे खिसक जाने वाला फ़ोकस
scroll-padding के बिना, Shift+Tab किसी लिंक को विंडो के बिल्कुल ऊपर, हेडर के पीछे, स्क्रॉल कर देता है, और देखकर कीबोर्ड चलाने वाला व्यक्ति उसे खो देता है। हेडर जितना ऊँचा पैडिंग हर फ़ोकस वाले एलिमेंट को उसके नीचे रखता है।
पहले से तय किया गया ऑफ़सेट
scroll-padding-top: 64px तब टूट जाता है जब हेडर फ़ोन पर दो पंक्तियों में बँट जाता है, सिकुड़ता है, या बड़े टेक्स्ट के साथ बढ़ता है। एक ResizeObserver हेडर को मापता है और पैडिंग को उसके बराबर रखता है।
हेडर के नीचे उतरने वाले जंप लिंक
स्किप या जंप लिंक अपने लक्ष्य को ऊपरी किनारे पर रख देता है, इसलिए जो हेडिंग उसे दिखानी थी वह हेडर के पीछे बैठ जाती है। वही पैडिंग, और लक्ष्यों पर scroll-margin, उन्हें दिखाई देने वाली जगह में उतारते हैं।
छिपा हुआ हेडर जिस पर अब भी फ़ोकस है
अगर हेडर तब खिसककर हट जाए जब उसके किसी लिंक पर फ़ोकस है, तो फ़ोकस ऐसी चीज़ पर रहता है जिसे कोई देख नहीं सकता। यहाँ फ़ोकस रहते हुए वह छिप नहीं सकता, और उसके अंदर किसी चीज़ पर फ़ोकस आते ही वह लौट आता है।
हेडर सिकुड़ने पर उछलने वाली सामग्री
फ़्लो में सिकुड़ने वाला हेडर अपने नीचे की हर चीज़ को ऊपर खींच लेता है, इसलिए टेक्स्ट उछलता है और सीमा पर हेडर झिलमिला सकता है। सिकुड़ने के साथ बढ़ने वाला नीचे का मार्जिन फ़्लो में उसकी जगह को एक-सा रखता है।
मोशन जिसे बंद नहीं किया जा सकता
हर स्क्रॉल पर खिसकने वाला हेडर स्क्रीन के किनारे पर लगातार चलता मोशन है। जब सिस्टम कम मोशन माँगता है तो यह एक ही क़दम में बदलता है।
नोट्स
इसे बनाना
असली पेज पर विंडो स्क्रॉल होती है: scroll-padding-top को html पर लगाएँ, हेडर को इसी तरह मापें, और स्क्रॉल क्षेत्र का tabindex और region रोल हटा दें। डेमो एक लेबल वाले क्षेत्र के अंदर स्क्रॉल होता है क्योंकि वह इस पेज के अंदर है।
डेमो में हेडर इस पेज के main के अंदर है, इसलिए वह banner लैंडमार्क नहीं है, और लैंडमार्क के नाम अलग रखने के लिए उसके नेविगेशन का नाम थाली है। आपकी साइट पर हेडर body का चाइल्ड होता है, नेविगेशन आपका मुख्य नेविगेशन होता है, और स्किप लिंक main की ओर इशारा करता है।
पैडिंग हेडर की पूरी ऊँचाई इस्तेमाल करता है, तब भी जब हेडर छिपा हो, क्योंकि ऊपर स्क्रॉल करने पर वह लौट आता है: छोटा मान फ़ोकस को वहाँ उतरने देगा जहाँ हेडर फिर से आने वाला है।
फ़ोकस बदलने पर ब्राउज़र scroll-padding लागू करते हैं; स्क्रिप्ट एक सुरक्षा जाल जोड़ती है, जो किसी ब्राउज़र के ऐसा न करने पर फ़ोकस वाले एलिमेंट को स्क्रॉल करके हेडर से बाहर ले आता है। यह उस स्क्रॉल को भी पलट देती है जो Chromium ब्राउज़र तब करते हैं जब Tab ख़ुद हेडर के किसी लिंक तक पहुँचता है, क्योंकि पैडिंग की वजह से वे उसे ढका हुआ मानते हैं।
छोटी ऊँचाई वाली विंडो पर ऊँचे हेडर का चिपकना बंद होने दें, जैसे max-height मीडिया क्वेरी से, जो उसे वापस position: static कर दे, ताकि 400% ज़ूम पर वह कभी आधी स्क्रीन न ढके।
लाइट और डार्क थीम में, डेस्कटॉप और फ़ोन की चौड़ाई पर axe से और कीबोर्ड से जाँचा गया।अभी हर स्क्रीन रीडर और ब्राउज़र जोड़ी के साथ नहीं परखा गया।सुधार बताएँ