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

CSS में पैरलैक्स स्क्रोलिंग प्रभाव कैसे बनाएं


परिचय

बैकग्राउंड और फोरग्राउंड कंटेंट के लिए अलग-अलग गति निर्दिष्ट करके, हम CSS का उपयोग करके पैरलैक्स स्क्रोलिंग प्रभाव बना सकते हैं। यह गहराई और दृश्य रुचि जोड़ता है।

उदाहरण: CSS पैरलैक्स स्क्रोलिंग

<!DOCTYPE html>
<html>
<head>
  <style>
    body, html {
      height: 100%;
      text-align: center;
    }
    .example {
      height: 100%;
      background-attachment: fixed;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    #demo {
      background: url("https://images.unsplash.com/photo-1612698565524-1101522e9a21?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=600&ixlib=rb-1.2.1&q=80&w=800");
    }
    #dist {
      height: 150px;
      background-color: rgba(102,234,99,0.5);
      padding: 2%;
    }
    #d2 {
      background: url("https://images.unsplash.com/photo-1611489473774-5d881f1153f1?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=600&ixlib=rb-1.2.1&q=80&w=800");
    }
  </style>
</head>
<body>
  <div id="demo" class="example"></div>
  <div id="dist"> Sed a porttitor enim. Integer euismod dui massa, at posuere sem fermentum at. Fusce vehicula fringilla tortor, ut mattis sapien tristique ac. Phasellus eleifend malesuada orci, et facilisis arcu egestas vel. Duis in tempus libero. Nunc nibh arcu, tempus quis lectus ut, blandit tincidunt felis. Suspendisse potenti. </div>
  <div id="d2" class="example"></div>
</body>
</html>

मुख्य तकनीक: background-attachment: fixed

  • बैकग्राउंड इमेज को व्यूपोर्ट के सापेक्ष **फिक्स्ड** स्थिति में रखता है
  • फोरग्राउंड कंटेंट सामान्य रूप से स्क्रॉल होता है
  • गहराई/पैरलैक्स का भ्रम पैदा करता है
  • background-size: cover के साथ सबसे अच्छा काम करता है

मॉडर्न दृष्टिकोण: CSS ट्रांसफॉर्म पैरलैक्स (बेहतर प्रदर्शन)

<!DOCTYPE html>
<html>
<head>
  <style>
    .parallax-wrapper {
      height: 100vh;
      overflow-x: hidden;
      overflow-y: auto;
      perspective: 1px;
      transform-style: preserve-3d;
    }
    .parallax-layer {
      position: relative;
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 3rem;
      color: white;
      text-shadow: 0 2px 10px rgba(0,0,0,0.5);
    }
    .layer-base {
      background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
      transform: translateZ(0);
    }
    .layer-back {
      background: url('bg.jpg') center/cover;
      transform: translateZ(-1px) scale(2);
    }
    .layer-deep {
      background: url('deep-bg.jpg') center/cover;
      transform: translateZ(-2px) scale(3);
    }
  </style>
</head>
<body>
  <div class="parallax-wrapper">
    <div class="parallax-layer layer-deep">गहरी परत</div>
    <div class="parallax-layer layer-back">पिछली परत</div>
    <div class="parallax-layer layer-base">आधार कंटेंट</div>
  </div>
</body>
</html>

व्यावहारिक उदाहरण: हीरो सेक्शन पैरलैक्स

<!DOCTYPE html>
<html lang="hi">
<head>
  <style>
    .hero-parallax {
      height: 100vh;
      min-height: 600px;
      position: relative;
      overflow: hidden;
    }
    .hero-parallax::before {
      content: '';
      position: absolute;
      top: 0; left: 0; right: 0; bottom: 0;
      background: url('hero-bg.jpg') center/cover;
      background-attachment: fixed;
      will-change: transform;
      z-index: -1;
    }
    .hero-content {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100%;
      color: white;
      text-align: center;
      padding: 2rem;
    }
    .hero-content h1 { font-size: clamp(2rem, 6vw, 4rem); margin-bottom: 1rem; }
    .hero-content p { font-size: clamp(1rem, 2.5vw, 1.5rem); max-width: 600px; }
    @media (prefers-reduced-motion: reduce) {
      .hero-parallax::before { background-attachment: scroll; }
    }
  </style>
</head>
<body>
  <section class="hero-parallax">
    <div class="hero-content">
      <h1>पैरलैक्स हीरो सेक्शन</h1>
      <p>स्क्रॉल करके पैरलैक्स प्रभाव देखें। बैकग्राउंड फोरग्राउंड की तुलना में धीमी गति से चलता है।</p>
    </div>
  </section>
  <section style="height: 100vh; background: white; display:flex; align-items:center; justify-content:center;">
    <p>सामान्य कंटेंट सेक्शन</p>
  </section>
</body>
</html>

प्रदर्शन नोट्स

  • background-attachment: fixed मोबाइल पर जंकी हो सकता है (कई ब्राउज़र्स इसे मोबाइल पर अक्षम करते हैं)
  • 3D ट्रांसफॉर्म दृष्टिकोण (perspective + translateZ) GPU-त्वरित और स्मूथ है
  • will-change: transform ब्राउज़र को ऑप्टिमाइज़ करने का संकेत देता है
  • हमेशा prefers-reduced-motion का सम्मान करें
  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, Geneva

  1. CSS में बॉर्डर इमेज कैसे बनाएं CSS में बॉर्डर इमेज कैसे बनाएं

    CSS में बॉर्डर इमेज बनाने के लिए, border-image प्रॉपर्टी का उपयोग करें। CSS में बॉर्डर इमेज बनाने का कोड निम्न है -उदाहरण<!DOCTYPE html> <html> <head> <style> body {    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .border1 {    

  1. CSS में पैरलैक्स स्क्रोलिंग प्रभाव कैसे बनाएं CSS में पैरलैक्स स्क्रोलिंग प्रभाव कैसे बनाएं

    परिचय बैकग्राउंड और फोरग्राउंड कंटेंट के लिए अलग-अलग गति निर्दिष्ट करके, हम CSS का उपयोग करके पैरलैक्स स्क्रोलिंग प्रभाव बना सकते हैं। यह गहराई और दृश्य रुचि जोड़ता है। उदाहरण: CSS पैरलैक्स स्क्रोलिंग <!DOCTYPE html> <html> <head> <style> body, html { height: 10