Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> CSS

CSS position:sticky में Intersection Observer का उपयोग करके एलिमेंट फिक्स होने का पता लगाना


परिचय

स्टिकी पोजिशन वाले एलिमेंट पर विभिन्न CSS स्टाइल्स लागू करके, हम आसानी से पता लगा सकते हैं कि यह कब "चिपक" जाता है। यह जावास्क्रिप्ट Intersection Observer API का उपयोग करके एक सेंटिनल एलिमेंट को देखकर करता है।

उदाहरण: स्टिकी स्टेट डिटेक्शन

<!DOCTYPE html>
<html>
<head>
  <style>
    #first {
      background-color: lightgrey;
      height: 10px;
    }
    #navbar-top {
      background-color: lightgrey;
      height: 2px;
    }
    #container {
      position: sticky;
      top: 0;
      box-shadow: inset 0 0 25px navy;
      height: 55px;
      text-align: center;
      font-size: 24px;
      line-height: 55px;
      font-weight: bold;
      transition: font-size 0.4s ease-in;
    }
    .sticky-navbar {
      box-shadow: inset 0 0 15px orange !important;
      font-size: 20px !important;
    }
    #parent-container {
      background-color: aliceblue;
      height: 3300px;
    }
  </style>
</head>
<body>
  <div id="first"></div>
  <div id="navbar-top"></div>
  <div id="container">Watch Me!</div>
  <div id="parent-container"></div>
  <script>
    let newObserver = new IntersectionObserver(function(entries) {
      if (entries[0].intersectionRatio === 0)
        document.querySelector("#container").classList.add("sticky-navbar");
      else if (entries[0].intersectionRatio === 1)
        document.querySelector("#container").classList.remove("sticky-navbar");
    }, { threshold: [0, 1] });
    newObserver.observe(document.querySelector("#navbar-top"));
  </script>
</body>
</html>

यह कैसे काम करता है

  1. सेंटिनल एलिमेंट: एक छोटा एलिमेंट (#navbar-top) स्टिकी एलिमेंट के ठीक ऊपर रखा जाता है
  2. Intersection Observer: सेंटिनल के व्यूपोर्ट के साथ इंटरसेक्शन को देखता है
  3. थ्रेशोल्ड [0, 1]: दो स्थितियों को ट्रिगर करता है:
    • ratio === 0: सेंटिनल पूरी तरह से दृश्य से बाहर → स्टिकी एलिमेंट **चिपका हुआ है**
    • ratio === 1: सेंटिनल पूरी तरह से दृश्य में → स्टिकी एलिमेंट **चिपका नहीं है**
  4. क्लास टॉगल: स्टिकी स्टेट के आधार पर विजुअल स्टाइल्स जोड़ता/हटाता है

व्यावहारिक उपयोग के मामले

1. स्टिकी हेडर शैडो/बैकग्राउंड चेंज

<!DOCTYPE html>
<html>
<head>
  <style>
    .sentinel { height: 1px; }
    .sticky-header {
      position: sticky; top: 0;
      background: white;
      padding: 1rem 2rem;
      display: flex; justify-content: space-between;
      transition: box-shadow 0.3s, background 0.3s;
      z-index: 100;
    }
    .sticky-header.is-stuck {
      box-shadow: 0 4px 20px rgba(0,0,0,0.1);
      background: rgba(255,255,255,0.95);
      backdrop-filter: blur(8px);
    }
  </style>
</head>
<body>
  <div class="sentinel" id="sentinel"></div>
  <header class="sticky-header" id="header">
    <div>Logo</div>
    <nav><a href="#">लिंक 1</a><a href="#">लिंक 2</a></nav>
  </header>
  <main><p>लंबा कंटेंट...</p></main>
  <script>
    const header = document.getElementById('header');
    new IntersectionObserver(([e]) => {
      header.classList.toggle('is-stuck', e.intersectionRatio === 0);
    }, { threshold: [0, 1] }).observe(document.getElementById('sentinel'));
  </script>
</body>
</html>

2. स्टिकी टेबल हेडर इंडिकेटर

<!DOCTYPE html>
<html>
<head>
  <style>
    table { width: 100%; border-collapse: collapse; }
    th { position: sticky; top: 0; background: #f8fafc; padding: 1rem; text-align: left; border-bottom: 2px solid #e2e8f0; transition: box-shadow 0.2s; }
    th.stuck { box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); }
    td { padding: 1rem; border-bottom: 1px solid #e2e8f0; }
    .table-sentinel { height: 1px; }
  </style>
</head>
<body>
  <div class="table-sentinel" id="table-sentinel"></div>
  <table>
    <thead><tr><th>नाम</th><th>ईमेल</th><th>भूमिका</th></tr></thead>
    <tbody>
      <tr><td>राहुल</td><td>rahul@example.com</td><td>एडमिन</td></tr>
      <tr><td>प्रिया</td><td>priya@example.com</td><td>संपादक</td></tr>
    </tbody>
  </table>
  <script>
    document.querySelectorAll('th').forEach(th => {
      new IntersectionObserver(([e]) => th.classList.toggle('stuck', e.intersectionRatio === 0), { threshold: [0, 1] })
        .observe(document.getElementById('table-sentinel'));
    });
  </script>
</body>
</html>

ब्राउज़र सपोर्ट

Intersection Observer: Chrome 51+, Firefox 55+, Safari 12.1+, Edge 79+। position: sticky: Chrome 56+, Firefox 32+, Safari 6.1+, Edge 79+।

  1. CSS पोजिशनिंग एलिमेंट्स (स्टेटिक, रिलेटिव, एब्सोल्यूट, फिक्स्ड) CSS पोजिशनिंग एलिमेंट्स (स्टेटिक, रिलेटिव, एब्सोल्यूट, फिक्स्ड)

    CSS position प्रॉपर्टी चार मान लेती है: static (डिफ़ॉल्ट, नॉर्मल फ्लो), relative (नॉर्मल फ्लो + ऑफसेट), absolute (पैरेंट के सापेक्ष, फ्लो से बाहर), fixed (व्यूपोर्ट के सापेक्ष, स्क्रॉल पर स्थिर)। उदाहरण <!DOCTYPE html> <html> <head> <style> body { font-family: Segoe U

  1. CSS से स्टिकी एलिमेंट कैसे बनाएं? CSS से स्टिकी एलिमेंट कैसे बनाएं?

    CSS से स्टिकी एलिमेंट बनाने के लिए, कोड निम्नानुसार है -उदाहरण <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style>    body {       font-family: "Segoe UI"

  1. CSS से फिक्स्ड/स्टिकी फुटर कैसे बनाएं? CSS से फिक्स्ड/स्टिकी फुटर कैसे बनाएं?

    CSS से फिक्स्ड फुटर बनाने के लिए, कोड निम्नानुसार है -उदाहरण<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style>    body{       font-family: 'Segoe UI', Tahoma, Ge