Simple Button Animation In TypeScript

Introduction

 
We learn how to change the color (background or foreground color) of the mouse during an event with the help of TypeScript.
 
The following example showS how to use button animation in a web application in TypeScript. In this example we use a Button control (buttonchange). We set the color (background and foreground color) as needed.
 
There are two TypeScript Functions in it:
  1. ChangeColor(): To change the color on the onmouseover event
  2. HideColor(): Sets the default color on the onmouseout event
Here we call the ChangeColor() function on the onmouseover event. In the ChangeColor() function, we call another function Change2(). This function is used to change the color of the button again.
 
Note: Here we call the setTimeout function; it helps us to call the function ( otherChange2() ) after 1 second (1000 milliseconds).
 
Now we call the HideColor() function on the onmouseout event. It sets the default color of the button. Let's use the following.
 
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click "HTML Application for TypeScript" under Visual C#.
 
Give the name of your application as "Button_Animation" and then click "Ok".
 
Step 2
After Step 1, right-click on "Button_Animation". A pop-up window is opened. Click on "Add" -> "New Item" -> "Web Form". Give the name of your WebForm as "Demo.aspx" and then click "Ok".
 
Step 3
After this session the project has been created; a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, CSS file, and aspx files and looks as in the following:
 

Coding

 
app.ts
  1. class Button_Animation {  
  2.  ChangeColor() {  
  3.   document.getElementById('buttonchange').style.backgroundColor = "Blue";  
  4.   document.getElementById('buttonchange').style.color = "White";  
  5.   setTimeout(() => {  
  6.    this.Change();  
  7.   }, 1000);  
  8.  }  
  9.  Change() {  
  10.   document.getElementById('buttonchange').style.backgroundColor = "Purple";  
  11.   document.getElementById('buttonchange').style.color = "White";  
  12.   setTimeout(() => {  
  13.    this.otherChange();  
  14.   }, 1000);  
  15.  }  
  16.  otherChange() {  
  17.   document.getElementById('buttonchange').style.backgroundColor = "Red";  
  18.   document.getElementById('buttonchange').style.color = "Black";  
  19.  }  
  20.  HideColor() {  
  21.   document.getElementById('buttonchange').style.backgroundColor = "green";  
  22.   document.getElementById('buttonchange').style.color = "Black";  
  23.  }  
  24. }  
  25. window.onload = () => {  
  26.  var obj = new Button_Animation();  
  27.  var changecolor = ( < HTMLButtonElement > document.getElementById("buttonchange"));  
  28.  changecolor.onmouseover = function() {  
  29.   obj.ChangeColor();  
  30.  };  
  31.  changecolor.onmouseout = function() {  
  32.   obj.HideColor();  
  33.  };  
  34. };  
Demo.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="simple_animation.Demo" %>  
  2.  <  
  3.  !DOCTYPE html >  
  4.  <  
  5.  html xmlns = "http://www.w3.org/1999/xhtml" >  
  6.  <  
  7.  head runat = "server" >  
  8.  <  
  9.  title > < /title>  
  10.  <  
  11.  script src = "app.js" > < /script>  
  12.  <  
  13.  style type = "text/css" > 
  14.  #buttonchange {  
  15.   width: 220 px;  
  16.   height: 103 px;  
  17.  }  
  18.  <  
  19.  /style>  
  20.  <  
  21.  /head>  
  22.  <  
  23.  body >  
  24.  <  
  25.  form id = "form1"  
  26. runat = "server" >  
  27.  <  
  28.  div >  
  29.  <  
  30.  asp: Label ID = "Label1"  
  31. runat = "server"  
  32. Font - Italic = "True"  
  33. Font - Size = "Large"  
  34. ForeColor = "#660033"  
  35. Text = "Simple Button Animation in WebApplication Using TypeScript" > < /asp:Label>  
  36.  <  
  37.  br / >  
  38.  <  
  39.  br / >  
  40.  <  
  41.  input id = "buttonchange"  
  42. type = "button"  
  43. value = "C# sharp corner" / >  
  44.  <  
  45.  /div>  
  46.  <  
  47.  /form>  
  48.  <  
  49.  /body>  
  50.  <  
  51.  /html>  
app.js
  1. var Button_Animation = (function() {  
  2.  function Button_Animation() {}  
  3.  Button_Animation.prototype.ChangeColor = function() {  
  4.   var _this = this;  
  5.   document.getElementById('buttonchange').style.backgroundColor = "Blue";  
  6.   document.getElementById('buttonchange').style.color = "White";  
  7.   setTimeout(function() {  
  8.    _this.Change();  
  9.   }, 1000);  
  10.  };  
  11.  Button_Animation.prototype.Change = function() {  
  12.   var _this = this;  
  13.   document.getElementById('buttonchange').style.backgroundColor = "Purple";  
  14.   document.getElementById('buttonchange').style.color = "White";  
  15.   setTimeout(function() {  
  16.    _this.otherChange();  
  17.   }, 1000);  
  18.  };  
  19.  Button_Animation.prototype.otherChange = function() {  
  20.   document.getElementById('buttonchange').style.backgroundColor = "Red";  
  21.   document.getElementById('buttonchange').style.color = "Black";  
  22.  };  
  23.  Button_Animation.prototype.HideColor = function() {  
  24.   document.getElementById('buttonchange').style.backgroundColor = "green";  
  25.   document.getElementById('buttonchange').style.color = "Black";  
  26.  };  
  27.  return Button_Animation;  
  28. })();  
  29. window.onload = function() {  
  30.  var obj = new Button_Animation();  
  31.  var b = (document.getElementById("buttonchange"));  
  32.  b.onmouseover = function() {  
  33.   obj.ChangeColor();  
  34.  };  
  35.  b.onmouseout = function() {  
  36.   obj.HideColor();  
  37.  };  
  38. };  
Output 1
 
first-image.jpg
 
Output 2
 
Mouse over on Button
 
After-change-function-image.jpg
 
Output 3
 
Hold mouse (1 millisecond) on Button
 
after-1-second-image.jpg
 
after-1-second-image-2.jpg
 
Output 4
 
Mouse out on Button
 
final-image.jpg
 
Referenced By
http://www.typescriptlang.org/


Similar Articles