Typing Text Effect

Written by @kerixa 17 May 2020

Dynamic text effects can change your site vision in the eyes of the visitors. Besides, it also attracts their attention to the auto-changing text and they follow it up to see what appears next. Therefore, we suggest to use the code below. Easy and interesting!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.javascriptfreecode.com coded by: Kerixa Inc. -->
<style>
@import url("https://fonts.googleapis.com/css?family=Public+Sans&display=swap");
body {
  margin: 0;
  padding: 0;
  font-family: "Public Sans", sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

img {
  width: 100px;
  height: 100px;
}

h1 {
  font-size: 68px;
  color: #747474;
}
h1 span.text {
  padding: 0;
  padding-right: 4px;
  border-right: 2px solid #747474;
  animation: blink 0.75s infinite;
}
h1 .blue {
  color: #4480ff;
}
h1 .green {
  color: #4caf50;
}
h1 .yellow {
  color: #f4b400;
}
h1 .red {
  color: #f44336;
}

p.info {
  font-size: 12px;
  margin-top: 6rem;
  color: #747474;
}
p.info a {
  text-decoration: none;
  color: #4480ff;
}

@keyframes blink {
  from {
    border: none;
  }
  to {
    border-color: 2px solid #747474;
  }
}
@media screen and (max-width: 670px) {
  h1 {
    font-size: 40px;
  }

  img {
    width: 75px;
    height: 75px;
  }
}
@media screen and (max-width: 400px) {
  h1 {
    font-size: 32px;
  }
}
</style>
<h1>Kerixa is <span class="text blue">Good</span></h1>
<script>
const words = ["Good", "Creative", "Novel", "Great"],
colors = ["blue", "green", "yellow", "red"],
text = document.querySelector(".text");

// Generator (iterate from 0-3)
function* generator() {
  var index = 0;
  while (true) {
    yield index++;

    if (index > 3) {
      index = 0;
    }
  }
}

// Printing effect
function printChar(word) {
  let i = 0;
  text.innerHTML = "";
  text.classList.add(colors[words.indexOf(word)]);
  let id = setInterval(() => {
    if (i >= word.length) {
      deleteChar();
      clearInterval(id);
    } else {
      text.innerHTML += word[i];
      i++;
    }
  }, 500);
}

// Deleting effect
function deleteChar() {
  let word = text.innerHTML;
  let i = word.length - 1;
  let id = setInterval(() => {
    if (i >= 0) {
      text.innerHTML = text.innerHTML.substring(0, text.innerHTML.length - 1);
      i--;
    } else {
      text.classList.remove(colors[words.indexOf(word)]);
      printChar(words[gen.next().value]);
      clearInterval(id);
    }
  }, 300);
}

// Initializing generator
let gen = generator();

printChar(words[gen.next().value]);
</script><a target='_blank' href='https://www.javascriptfreecode.com' style='font-size: 8pt; text-decoration: none'>JavaScript Best Codes</a>                                                
                                            

Example:


About @kerixa

I am Krishna Eydat. I studied Software Engineering at University of Waterloo in Canada. I lead a few tech companies. I am passionate about the way we are connected. I would like to be part of something big or be the big deal!

K

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: CryptoTech, AstroAudrey, admin_js, bloxio, yqaice
advertisement 2