परिचय
CSS scroll-behavior प्रॉपर्टी हमें स्क्रॉल के व्यवहार को बदलने की अनुमति देती है। जावास्क्रिप्ट के बिना, हम एंकर लिंक्स और प्रोग्रामेटिक स्क्रॉलिंग के लिए स्मूथ स्क्रॉलिंग सक्षम कर सकते हैं।
उदाहरण 1: वर्टिकल स्मूथ स्क्रॉल
<!DOCTYPE html>
<html>
<head>
<style>
html {
line-height: 200px;
margin: 30px;
text-align: center;
vertical-align: middle;
}
#parent {
scroll-behavior: smooth;
width: 250px;
height: 200px;
overflow-y: scroll
}
.pink {
height: 200px;
box-shadow: inset 0 0 50px deeppink;
}
.blue {
height: 200px;
box-shadow: inset 0 0 50px darkblue;
}
</style>
</head>
<body>
<div id="parent">
<div class="blue" id="first"><a href="#last">Last</a></div>
<div class="pink"></div>
<div class="blue"></div>
<div class="pink" id="last"><a href="#first">First</a></div>
</div>
</body>
</html>
उदाहरण 2: ऑटो स्क्रॉल व्यवहार (तत्काल)
<!DOCTYPE html>
<html>
<head>
<style>
html {
line-height: 150px;
margin: 30px;
text-align: center;
vertical-align: middle;
}
#parent {
scroll-behavior: auto;
width: 150px;
height: 150px;
overflow: hidden;
border: 22px groove tomato;
border-radius: 50%;
}
.green {
height: 150px;
box-shadow: inset 0 0 50px darkgreen;
}
.blue {
height: 150px;
box-shadow: inset 0 0 50px darkblue;
}
</style>
</head>
<body>
<div id="parent">
<div class="blue" id="first"><a href="#last">Last</a></div>
<div class="green"></div>
<div class="blue"></div>
<div class="green" id="last"><a href="#first">First</a></div>
</div>
</body>
</html>
स्क्रॉल-बिहेवियर मान
| मान | विवरण |
|---|---|
auto |
तत्काल स्क्रॉल (डिफ़ॉल्ट) |
smooth |
एनिमेटेड स्मूथ स्क्रॉल |
लागू होता है
<html>एलिमेंट पर (पेज-लेवल स्मूथ स्क्रॉल के लिए)- किसी भी स्क्रॉल कंटेनर पर (
overflow: scroll/auto) - एंकर लिंक्स (
<a href="#section">) पर - जावास्क्रिप्ट
element.scrollTo(),scrollIntoView()पर
व्यावहारिक कार्यान्वयन: पेज-वाइड स्मूथ स्क्रॉल
<!DOCTYPE html>
<html>
<head>
<style>
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto; /* मोशन सेंसिटिविटी का सम्मान करें */
}
}
section {
min-height: 100vh;
padding: 4rem 2rem;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
nav {
position: fixed;
top: 1rem;
right: 1rem;
display: flex;
gap: 0.5rem;
z-index: 1000;
}
nav a {
padding: 0.5rem 1rem;
background: #333;
color: white;
text-decoration: none;
border-radius: 4px;
}
</style>
</head>
<body>
<nav>
<a href="#section1">सेक्शन 1</a>
<a href="#section2">सेक्शन 2</a>
<a href="#section3">सेक्शन 3</a>
</nav>
<section id="section1" style="background: #e3f2fd;">सेक्शन 1</section>
<section id="section2" style="background: #fce4ec;">सेक्शन 2</section>
<section id="section3" style="background: #e8f5e9;">सेक्शन 3</section>
</body>
</html>
एक्सेसिबिलिटी नोट
हमेशा @media (prefers-reduced-motion: reduce) शामिल करें ताकि वेस्टिबुलर विकारों वाले उपयोगकर्ताओं के लिए एनिमेशन अक्षम हो जाए। यह WCAG 2.1 दिशानिर्देशों के अनुसार आवश्यक है।
ब्राउज़र सपोर्ट
Chrome 61+, Firefox 36+, Safari 15+, Edge 79+ में सपोर्टेड। Safari 15 से पहले -webkit- प्रीफिक्स की आवश्यकता होती है। IE में सपोर्ट नहीं है।