admin管理员组文章数量:1323540
I need to loop through the CSS typewriter effect, and change the text for each loop. Here is the code I'm using for the typewriter effect. I'm guessing I need to use javascript, but I am not sure how to go about this. Any ideas on how I could do this?
Snippet Reference
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
body {
background: #333;
color: #fff;
font-family: monospace;
padding-top: 5em;
display: flex;
justify-content: center;
}
/* DEMO-SPECIFIC STYLES */
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
/* The typing effect */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
/* The typewriter cursor effect */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: blue; }
}
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
I need to loop through the CSS typewriter effect, and change the text for each loop. Here is the code I'm using for the typewriter effect. I'm guessing I need to use javascript, but I am not sure how to go about this. Any ideas on how I could do this?
Snippet Reference
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
body {
background: #333;
color: #fff;
font-family: monospace;
padding-top: 5em;
display: flex;
justify-content: center;
}
/* DEMO-SPECIFIC STYLES */
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
/* The typing effect */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
/* The typewriter cursor effect */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: blue; }
}
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
Share
Improve this question
edited Mar 28, 2023 at 9:49
Anas Shakil
231 silver badge4 bronze badges
asked Mar 15, 2017 at 22:38
Kevin ShiflettKevin Shiflett
2131 gold badge4 silver badges10 bronze badges
1
- Just a note: your code is copy pasted from: css-tricks./snippets/css/typewriter-effect. Mentioning your source is a nice thing to do. :-) – Mr. Hugo Commented Sep 12, 2019 at 9:03
3 Answers
Reset to default 3Yes you will need some script, at least to detect the end of the animations. Then to refresh html content,you can store all the messages to display in an array and loop through it. Then you have to adjust the speed, depending on the length of the text displayed.
var messages=["message1","message2 message2 message2","message3 message3"];
var rank=0;
// Code for Chrome, Safari and Opera
document.getElementById("myTypewriter").addEventListener("webkitAnimationEnd", changeTxt);
// Standard syntax
document.getElementById("myTypewriter").addEventListener("animationend", changeTxt);
function changeTxt(e){
_h1 = this.getElementsByTagName("h1")[0];
_h1.style.webkitAnimation = 'none'; // set element animation to none
setTimeout(function() { // you surely want a delay before the next message appears
_h1.innerHTML=messages[rank];
var speed =3.5*messages[rank].length/20; // adjust the speed (3.5 is the original speed, 20 is the original string length
_h1.style.webkitAnimation = 'typing '+speed+'s steps(40, end), blink-caret .75s step-end infinite'; // switch to the original set of animation
(rank===messages.length-1)?rank=0:rank++; // if you have displayed the last message from the array, go back to the first one, else go to next message
}, 1000);
}
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
body {
background: #333;
color: #fff;
font-family: monospace;
padding-top: 5em;
display: flex;
justify-content: center;
}
/* The typing effect */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
/* The typewriter cursor effect */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: blue; }
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="typewriter" id="myTypewriter">
<h1>The cat and the hat.</h1>
</div>
also check for the different syntax https://www.w3schools./jsref/prop_style_animation.asp
This can be done via jQuery (or JavaSript, if you prefer). Simply, wait till the animation is done, then replace the containing HTML.
setTimeout(function () {
$(".typewriter").html("<h1>This is a really cool string</h1>")
},3500);
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
body {
background: #333;
color: #fff;
font-family: monospace;
padding-top: 5em;
display: flex;
justify-content: center;
}
/* DEMO-SPECIFIC STYLES */
.typewriter h1 {
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(40, end),
blink-caret .75s step-end infinite;
}
/* The typing effect */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
/* The typewriter cursor effect */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: blue; }
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
You do not NEED javascript for this.
Do not be fooled. This can be done with CSS only, see: https://stackoverflow./a/57887588/2397550 for a working example. You could split the CSS animation into several individual ones and execute them after each other with animation-delay
.
A(nother) downside to your solution is that it requires a monospaced font, which is a limitation I would not like to have.
本文标签: javascriptLoop CSS typewriter effect and change textStack Overflow
版权声明:本文标题:javascript - Loop CSS typewriter effect and change text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124114a2421859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论