Fadein and Fadeout effect through javascript

Sponsors

Many times we need the fadein and fadeout effect using javascript. But many developers go for jquery for using the simple fadein and fadeout effect. I suggest not to Jquery and Use following code for effect.  This is very minimal code and you can very easily customize the CSS and javascript as per your requirement.

<html>
 <head>

 <style type="text/css">

 .popup {
 border: solid 1px #333;
 font-family: Tahoma;
 font-size: 12px;
 display: none;
 position: absolute;
 width: 300px;
 z-index: 60;
 }

 .popuptitle {
 background: blue;
 color: white;
 font-weight: bold;
 height: 15px;
 padding: 5px;
 }

 .popupbody {
 background: #ddd;
 padding: 5px;
 text-align: center;
 }

 #popup1 { top: 100px; left: 50px; }

 #popup2 { top: 100px; left: 400px; }

 </style>

 <script type="text/javascript">

 var fadeOpacity  = new Array();
 var fadeTimer    = new Array();
 var fadeInterval = 100;  // milliseconds

 function fade(o,d)
 {

 // o - Object to fade in or out.
 // d - Display, true =  fade in, false = fade out

 var obj = document.getElementById(o);

 if((fadeTimer[o])||(d&&obj.style.display!='block')||(!d&&obj.style.display=='block'))
 {

 if(fadeTimer[o])
 clearInterval(fadeTimer[o]);
 else
 if(d) fadeOpacity[o] = 0;
 else  fadeOpacity[o] = 9;

 obj.style.opacity = "."+fadeOpacity[o].toString();
 obj.style.filter  = "alpha(opacity="+fadeOpacity[o].toString()+"0)";

 if(d)
 {
 obj.style.display = 'block';
 fadeTimer[o] = setInterval('fadeAnimation("'+o+'",1);',fadeInterval);
 }
 else
 fadeTimer[o] = setInterval('fadeAnimation("'+o+'",-1);',fadeInterval);
 }
 }

 function fadeAnimation(o,i)
 {

 // o - o - Object to fade in or out.
 // i - increment, 1 = Fade In

 var obj = document.getElementById(o);
 fadeOpacity[o] += i;
 obj.style.opacity = "."+fadeOpacity[o].toString();
 obj.style.filter  = "alpha(opacity="+fadeOpacity[o].toString()+"0)";

 if((fadeOpacity[o]=='9')|(fadeOpacity[o]=='0'))
 {
 if(fadeOpacity[o]=='0')
 obj.style.display = 'none';
 else
 {
 obj.style.opacity = "1";
 obj.style.filter  = "alpha(opacity=100)";
 }

 clearInterval(fadeTimer[o]);
 delete(fadeTimer[o]);
 delete(fadeTimer[o]);
 delete(fadeOpacity[o]);

 }
 }

 </script>

 </head>
 <body>

 <input type="button" value="Show Popup 1" onClick="fade('popup1',true);"/>
 <input type="button" value="Show Popup 2" onClick="fade('popup2',true);"/>

 <div id="popup1">
 <div>Fade Popup 1</div>
 <div>
 <p>Press close to fade out this Popup</p>
 <input type="button" value="Close" onClick="fade('popup1',false);"/>
 </div>
 </div>

 <div id="popup2">
 <div>Fade Popup 2</div>
 <div>
 <p>Press close to fade out this Popup</p>
 <input type="button" value="Close" onClick="fade('popup2',false);"/>
 </div>
 </div>

 </body>

</html>

Incoming search terms:

You may like following Articles!

This entry was posted in JavaScript and tagged , by Wordpress API. Bookmark the permalink.

About Wordpress API

API means Application Program interface. In Wordpress API we are giving all the information about wordpress API. We worked for many wordpress plugins and themes. We have expertise in PHP, Databases, Graphic Design, Ruby on Rails, Java, Wordpress, Drupal and many CMS. We always give you the best solutions about wordpress and other related topics in This Blog. We will give you always fresh tips and tricks and techniques about wordpress in this blog.

12 thoughts on “Fadein and Fadeout effect through javascript

  1. I am grateful for this particular ideal commentary; this is the kind of point that prevents me though out the day.I’ve been hunting close to for ones web site soon after I noticed about them from a friend and was delighted when I was capable of come across it just after browsing for a while. Becoming a experienced blogger, I’m thrilled to see other folks taking gumption and adding to your neighborhood. I just needed to comment to display my appreciation on your submit as it is especially inviting, and many freelancers will not get the credit score they ought to have. I am positive I’ll be back again and will send out some of my friends.

  2. Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. I’ll tell you about that later…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>