SIGN UP MEMBER LOGIN:    
ARTICLE

Simple Animation in JavaScript

Posted by Mahak Gupta Articles | ASP, JavaScript, CSS June 27, 2011
Here we create a simple animation in JavaScript. When we mouseover on particular text, it will change the image according to where the mouseover occurs.
Reader Level:


Here we create a simple animation in JavaScript. When we mouseover on particular text, it will change the image according to where the mouseover occurs.

AnimaJavaS1.gif

In this example, I prefer One,Two Three, you can choose whatever heading you prefer. In this example we change the color and image on the onmouse over of the particular heading.

    <table cellpadding="0" cellspacing="0" style="width: 309px" border="2">
    <tr>
    <td class="style1"><img id="img1" /></td>
    <td
>

    <table style="width: 276px; height: 147px; margin-left: 5px">
    <tr>
    <td id="td1" class="style2" onmouseover="Show1()"  onmouseout="Hide1()"
            style="border-style: solid; border-width: 1px">One</td>
    </tr>
    <tr>
    <td id="td2" onmouseover="Show2()"  onmouseout="Hide2()" class="style2" style="border-style: solid; border-width: 1px" >Two</td>
    </tr>
    <tr>
    <td id="td3" onmouseover="Show3()"  onmouseout="Hide3()"  class="style2" style="border-style: solid; border-width: 1px" >Three</td>
    </tr>
    </table
>

    </td>
    </tr>
    </table>
    </div>
    </form
>


AnimaJavaS2.gif

In this example, there are the two functions Show2() and Hide2() of JavaScript.

Show2(): This function is called on the mouseover of the particular heading (here td1, td2, td3). With this we change the color (foreground and background) of the td and change the image.

Hide2(): This function is called for the onmouseout event of the particular heading (here td1, td2, td3). With this we set the default color of the heading.

Note: Here we take an Image Folder to store our images.

Now we write the JavaScript functions:

<script language="javascript" type="text/javascript">
        function Show1() {
            document.getElementById('img1').src = "Image/one.jpg";
            document.getElementById('td1').style.color = "Red";
            document.getElementById('td1').style.backgroundColor = "#FF9999";
            document.getElementById('td1').style.fontStyle = "Italic";

        }
        function Hide1() {
            document.getElementById('td1').style.color = "Black";
            document.getElementById('td1').style.backgroundColor = "#FFFFFF";
            document.getElementById('td1').style.borderColor = "Black";
            document.getElementById('td1').style.fontStyle = "";

        }

        function Show2() {
            document.getElementById('img1').src = "Image/two.jpg";
            document.getElementById('td2').style.color = "Red";
            document.getElementById('td2').style.backgroundColor = "#FF9999";
            document.getElementById('td2').style.fontStyle = "Italic";

        }
        function Hide2() {
            document.getElementById('td2').style.color = "Black";
            document.getElementById('td2').style.backgroundColor = "#FFFFFF";
            document.getElementById('td2').style.borderColor = "Black";
            document.getElementById('td2').style.fontStyle = "";

        }
        function Show3() {
            document.getElementById('img1').src = "Image/Three.png";
            document.getElementById('td3').style.color = "Red";
            document.getElementById('td3').style.backgroundColor = "#FF9999";
            document.getElementById('td3').style.fontStyle = "Italic";

        }
        function Hide3() {
            document.getElementById('td3').style.color = "Black";
            document.getElementById('td3').style.backgroundColor = "#FFFFFF";
            document.getElementById('td3').style.borderColor = "Black";
            document.getElementById('td3').style.fontStyle = "";

        }

Here we set the source of the Image:

document.getElementById('img1').src = "Image/Three.png";

And the style of the heading:

document.getElementById('td3').style.color = "Red";
document.getElementById('td3').style.backgroundColor = "#FF9999";
document.getElementById('td3').style.fontStyle = "Italic";


Complete Program:

<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function Show1() {
            document.getElementById('img1').src = "Image/one.jpg";
            document.getElementById('td1').style.color = "Red";
            document.getElementById('td1').style.backgroundColor = "#FF9999";
            document.getElementById('td1').style.fontStyle = "Italic";

        }
        function Hide1() {
            document.getElementById('td1').style.color = "Black";
            document.getElementById('td1').style.backgroundColor = "#FFFFFF";
            document.getElementById('td1').style.borderColor = "Black";
            document.getElementById('td1').style.fontStyle = "";
 
        }
 
        function Show2() {
            document.getElementById('img1').src = "Image/two.jpg";
            document.getElementById('td2').style.color = "Red";
            document.getElementById('td2').style.backgroundColor = "#FF9999";
            document.getElementById('td2').style.fontStyle = "Italic";

        }
        function Hide2() {
            document.getElementById('td2').style.color = "Black";
            document.getElementById('td2').style.backgroundColor = "#FFFFFF";
            document.getElementById('td2').style.borderColor = "Black";
            document.getElementById('td2').style.fontStyle = "";

        }
        function Show3() {
            document.getElementById('img1').src = "Image/Three.png";
            document.getElementById('td3').style.color = "Red";
            document.getElementById('td3').style.backgroundColor = "#FF9999";
            document.getElementById('td3').style.fontStyle = "Italic";

        }
        function Hide3() {
            document.getElementById('td3').style.color = "Black";
            document.getElementById('td3').style.backgroundColor = "#FFFFFF";
            document.getElementById('td3').style.borderColor = "Black";
            document.getElementById('td3').style.fontStyle = "";
        } 
   
    </script>
    <style type="text/css">
        .style1
        {
            width: 195px;
        }
        #img1
        {
            width: 138px;
        }
        .style2
        {
            text-align: center;
        }
    </style
>
</head>
<
body style="height: 173px">
    <form id="form1" runat="server">
    <div>
    <table cellpadding="0" cellspacing="0" style="width: 309px" border="2">
    <tr>
    <td class="style1"><img id="img1" /></td>
    <td>
    <table style="width: 276px; height: 147px; margin-left: 5px">
    <tr>
    <td id="td1" class="style2" onmouseover="Show1()"  onmouseout="Hide1()"
            style="border-style: solid; border-width: 1px">One</td>
    </tr>
    <tr>
    <td id="td2" onmouseover="Show2()"  onmouseout="Hide2()" class="style2" style="border-style: solid; border-width: 1px" >Two</td>
    </tr>
    <tr>
    <td id="td3" onmouseover="Show3()"  onmouseout="Hide3()"  class="style2" style="border-style: solid; border-width: 1px" >Three</td>
    </tr>
    </table
>

    </td>
    </tr>
    </table>
    </div>
    </form
>
</body>

Login to add your contents and source code to this article
share this article :
post comment
 

nice one sir, please give me other best examples of simple validation using java script

Posted by manoj bhatt Jul 20, 2011

U r posting java script snippet so clearly that i love to read..thanks a lot keep posting :-)

Posted by surender bhyan Jun 27, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor