Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- loop between a number of sentences in a typewriter style -->
- <div id="mysentences"></div>
- <style>
- #mysentences {
- font-family: Verdana, Geneva, sans-serif;
- font-size: 30px;
- letter-spacing: 2px;
- word-spacing: 2px;
- color: #000000;
- font-weight: 400;
- text-decoration: none;
- font-style: normal;
- font-variant: normal;
- text-transform: none;
- }
- </style>
- <script>
- const jamessentences = [
- "FROM ‘IF’ BY RUDYARD KIPLING (1910)",
- "If you can keep your head when all about you",
- "Are losing theirs and blaming it on you,",
- "If you can trust yourself when all men doubt you,",
- "But make allowance for their doubting too;",
- "If you can wait and not be tired by waiting,",
- "Or being lied about, don’t deal in lies,",
- "Or being hated, don’t give way to hating,",
- "And yet don’t look too good, nor talk too wise:",
- ];
- const jamesspeed = 100; // time in milliseconds between each letter
- const jamespause = 2000; // time in milliseconds to pause
- const jamesloop = 0; // number of times to display sentences (0 = infinite)
- typeWriter(0,jamessentences,0,jamesspeed,jamespause,jamesloop);
- function typeWriter(i,txt,texti,speed,pause,loop) {
- if (i==0) document.getElementById('mysentences').innerHTML = '';
- if (i < txt[texti].length) {
- document.getElementById('mysentences').innerHTML += txt[texti].charAt(i);
- i++;
- setTimeout(function() {
- typeWriter(i,txt,texti,speed,pause,loop);
- }, speed);
- } else {
- i = 0;
- texti++;
- if (texti == txt.length) {
- loop--;
- if (loop == 0) return;
- texti = 0;
- }
- setTimeout(function() {
- cleanUpTypeWriter(i,txt,texti,speed,pause,loop);
- }, pause);
- }
- }
- function cleanUpTypeWriter(i,txt,texti,speed,pause,loop) {
- if (document.getElementById('mysentences').innerHTML == '') {
- typeWriter(i,txt,texti,speed,pause,loop);
- } else {
- document.getElementById('mysentences').innerHTML = document.getElementById('mysentences').innerHTML.slice(0, -1);
- setTimeout(function() {
- cleanUpTypeWriter(i,txt,texti,speed,pause,loop);
- }, speed/10);
- }
- }
- </script>
Add Comment
Please, Sign In to add comment