परिचय
CSS एनिमेशन की मदद से, हम जावास्क्रिप्ट का उपयोग करके टाइपराइटर एनिमेशन बना सकते हैं। यह प्रभाव टेक्स्ट को कैरेक्टर दर कैरेक्टर टाइप होते हुए दिखाता है, जिसमें ब्लिंकिंग कर्सर होता है।
उदाहरण: CSS-ओनली टाइपराइटर प्रभाव
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 2%;
font-family: Courier, monospace;
display: inline-block;
}
div > div {
overflow: hidden;
animation: enter 4s steps(30, end), blinker 0.65s step-end infinite;
white-space: nowrap;
font-size: 1.4em;
border-right: 5px solid blue;
}
@keyframes enter {
0% { width: 0%; }
100% { width: 100%; }
}
@keyframes blinker {
0%, 100% { border-color: transparent; }
50% { border-color: blue; }
}
</style>
</head>
<body>
<div class="typewriter">
<div class="typewriter-text">This is what you've been waiting for..</div>
</div>
</body>
</html>
यह कैसे काम करता है
steps(30, end)— टेक्स्ट लंबाई (30 कैरेक्टर्स) के आधार पर चरणों में एनिमेट करता हैoverflow: hidden+widthएनिमेशन — टाइपिंग प्रभाव बनाता हैborder-right+blinkerएनिमेशन — कर्सर ब्लिंक बनाता हैwhite-space: nowrap— लाइन रैपिंग रोकता हैfont-family: monospace— समान कैरेक्टर चौड़ाई सुनिश्चित करता है
मल्टी-लाइन टाइपराइटर (एडवांस्ड)
<!DOCTYPE html>
<html>
<head>
<style>
.typewriter-multi {
font-family: 'Fira Code', monospace;
font-size: 1.5rem;
color: #e0e0e0;
background: #1e1e1e;
padding: 2rem;
border-radius: 8px;
max-width: 600px;
margin: 2rem auto;
overflow: hidden;
border-right: 3px solid #3b82f6;
}
.typewriter-multi .line {
height: 1.5em;
overflow: hidden;
white-space: nowrap;
}
.typewriter-multi .line:nth-child(1) {
animation: type1 2s steps(22, end) forwards,
blink 0.75s step-end infinite 2s;
}
.typewriter-multi .line:nth-child(2) {
opacity: 0;
animation: type2 2s steps(18, end) 2s forwards,
blink 0.75s step-end infinite 4s;
}
.typewriter-multi .line:nth-child(3) {
opacity: 0;
animation: type3 1.5s steps(15, end) 4s forwards,
blink 0.75s step-end infinite 5.5s;
}
@keyframes type1 { from { width: 0; } to { width: 100%; opacity: 1; } }
@keyframes type2 { from { width: 0; opacity: 0; } to { width: 100%; opacity: 1; } }
@keyframes type3 { from { width: 0; opacity: 0; } to { width: 100%; opacity: 1; } }
@keyframes blink { 50% { border-color: transparent; } }
</style>
</head>
<body>
<div class="typewriter-multi">
<div class="line">const greeting = 'Hello World';</div>
<div class="line">console.log(greeting);</div>
<div class="line">// Output: Hello World</div>
</div>
</body>
</html>
जावास्क्रिप्ट एन्हांस्ड वर्जन (डायनामिक टेक्स्ट)
<!DOCTYPE html>
<html>
<head>
<style>
.typewriter-js {
font-family: monospace;
font-size: 1.25rem;
border-right: 2px solid #3b82f6;
white-space: nowrap;
overflow: hidden;
display: inline-block;
}
.typewriter-js.typing {
animation: blink 0.75s step-end infinite;
}
@keyframes blink { 50% { border-color: transparent; } }
</style>
</head>
<body>
<div class="typewriter-js" id="typewriter"></div>
<script>
const texts = [
'CSS के साथ टाइपराइटर प्रभाव',
'शुद्ध CSS एनिमेशन शक्तिशाली हैं',
'जावास्क्रिप्ट के साथ डायनामिक कंटेंट'
];
const el = document.getElementById('typewriter');
let textIndex = 0, charIndex = 0, isDeleting = false;
function type() {
const current = texts[textIndex];
if (isDeleting) {
el.textContent = current.slice(0, charIndex--);
} else {
el.textContent = current.slice(0, charIndex++);
}
el.classList.toggle('typing', !isDeleting);
let speed = isDeleting ? 50 : 100;
if (!isDeleting && charIndex === current.length) {
speed = 2000; isDeleting = true;
} else if (isDeleting && charIndex === 0) {
isDeleting = false; textIndex = (textIndex + 1) % texts.length; speed = 500;
}
setTimeout(type, speed);
}
type();
</script>
</body>
</html>
एक्सेसिबिलिटी विचार
prefers-reduced-motionका सम्मान करें — एनिमेशन अक्षम करें- स्क्रीन रीडर्स के लिए
aria-live="polite"प्रदान करें - पूरा टेक्स्ट वैकल्पिक रूप से उपलब्ध सुनिश्चित करें