Javascript - Simple Text Animation

In this tutorial we will create a Simple Text Animation using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding.

Before we started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/

The Main Interface

This code contains the interface of the application. This will display the text that will be needed to animate within the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascript - Simple Text Animation</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-2"></div>
  17. <div class="col-md-8">
  18. <label>Enter a text</label>
  19. <input type="text" name="text" id="inputText" style="width:140px;"/>
  20. <button class="btn btn-primary" onclick="decreaseFont()"><span class="glyphicon glyphicon-minus"></span></button>
  21. <input type="text" id="size" style="width:50px;" disabled = "disabled"/>
  22. <button class="btn btn-primary" onclick="increaseFont()"><span class="glyphicon glyphicon-plus"></span></button>
  23. <br /><br />
  24. <center><button class="btn btn-success" onclick="changeColor()">Change Color</button></center>
  25. <br /><br /><br />
  26. <center><label id="display"></label></center>
  27. </div>
  28. </div>
  29. </body>
  30. <script src="script.js"></script>
  31. </html>

Creating the Script

This code contains the script of the application. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js.
  1. var size = 14;
  2. var color = "#000000"
  3. var data = document.getElementById("inputText");
  4. var display = document.getElementById('display');
  5. var font = document.getElementById('size');
  6. var arrayColor=["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#000000"]
  7.  
  8. display.style.fontSize= size + "px";
  9. display.style.color=color;
  10. font.value = size;
  11. data.onkeyup = function(){
  12. display.innerHTML = inputText.value;
  13. }
  14.  
  15. function increaseFont(){
  16. size++;
  17. display.style.fontSize= size + "px";
  18. font.value = size;
  19. }
  20.  
  21. function decreaseFont(){
  22. size--;
  23.  
  24. if(size < 1){
  25. size = 1;
  26. }
  27.  
  28. display.style.fontSize= size + "px";
  29. font.value = size;
  30. }
  31.  
  32. function changeColor(){
  33. var rand = arrayColor[Math.floor(Math.random() * arrayColor.length)];
  34. display.style.color=rand;
  35. }
There you have it we successfully created a Simple Text Animation using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment