Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » JavaScript » How to change the image background in JavaScript

How to change the image background in JavaScript

Through this article you will get to know the concept of events in JavaScript. By using the events (onMouseOver and onMouseOut) you can change the image background.

Page Views : 19243
Downloads : 86
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
IMG_BGCOl.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction:

 

Through this article you will get the knowledge of events. You can change the image background using some events. In this article you will see an example to change the image background.

  

What is Events?

 

Events are the actions that can be detected by JavaScript. Events are normally used in combination with functions, and the function will not be executed before the event occurs.

 

These are the following events in JavaScript.

 

  • onload and onUnload
  • onSubmit
  • onFocus, onBlur and onChange
  • onMouseOver and onMouseOut

In this article you will see the use of onMouseOver and onMouseOut events.

 

How to change image background using JavaScript?

 

To change the image background use a function within the <script> and </script> tags something like this:

 

<script type="text/javascript">

  function imagechange(imgName)

  {

   document.body.background = eval(imgName + ".src");

  }

</script>

 

Example to change Image background:  

 

<html>

  <head>

    <title>Change_image_background</title>

    <script type="text/javascript">

    clear = new Image();

    clear.src = "clear.jpg";

    pic1 = new Image();

    pic1.src = "image1.jpg"

    pic2 = new Image();

    pic2.src = "image2.jpg";

    pic3 = new Image();

    pic3.src = "bigcar.jpg";

    function imagechange(imgName)

    {

      document.body.background = eval(imgName + ".src");

    }

    </script>

  </head>

  <body>

    <div align=center>

      <a href="image1.jpg" onMouseOver="imagechange('pic1');" onMouseOut="imagechange('clear');">

      <img src="image1.jpg"height=70 width=70></a>

      <a href="image2.jpg" onMouseOver="imagechange('pic2');" onMouseOut="imagechange('clear');">

      <img src="image2.jpg"height=70 width=70></a>

      <a href="bigcar.jpg" onMouseOver="imagechange('pic3');" onMouseOut="imagechange('clear');">

      <img src="bigcar.jpg" height=70 width=70></a>

    </div>

  </body>

</html>

 

In this example you have to see the mouse events (like onMOuseOver and onMouseOut). onMouseOver event will be fire when you put the cursor over the image and onMOuseOut event will be fire when out the cursor from the image.

 

Output:

 

 

 

Figure 1: Output of the script.

 

If you put your cursor over the image then background will be change like this:

 

 

 

Figure 2: Image is appearing in the background.

 

Similarly other images will be appear when you put the mouse on those images.

When you remove the cursor from the image then background will we clear.

 

If you click on any image then that image will be open onto another page like this:

 

 

 

Figure 3: Second image is displaying onto another page.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purshottam Rathore
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.