Advertisement
brandizzi

Fade in/fade out manually in JavaScript

Mar 28th, 2012
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.41 KB | None | 0 0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5.     fadeIn = function(self) {
  6.         var opacity = self.style.opacity;
  7.         if (opacity < 1) {
  8.            if (!opacity) opacity = 0.4;
  9.            opacity = parseFloat(opacity)+0.05;
  10.            self.style.opacity = opacity;
  11.            setTimeout(function() {
  12.              fadeIn(self);
  13.            }, 50);  
  14.        }
  15.    }
  16.    fadeOut = function(self) {
  17.        var opacity = self.style.opacity;
  18.        if (opacity > 0.4) {
  19.             if (!opacity) opacity = 1.0;
  20.             opacity = parseFloat(opacity) - 0.05;
  21.             self.style.opacity = opacity;
  22.             setTimeout(function() {
  23.               fadeOut(self);
  24.             }, 100);  
  25.         }
  26.     }
  27. </script>
  28. <style type="text/css">
  29. img
  30. {
  31. opacity:0.4;
  32. filter:alpha(opacity=40); /* For IE8 and earlier */
  33. }
  34. /*img:hover
  35. {
  36. opacity:1.0;
  37. filter:alpha(opacity=100); * For IE8 and earlier *
  38. }*/
  39. </style>
  40. </head>
  41. <body>
  42.  
  43. <h1>Image Transparency</h1>
  44. <img src="klematis.jpg" onmouseover="fadeIn(this)" onmouseout="fadeOut(this)" width="150" height="113" alt="klematis" />
  45. <img src="klematis2.jpg" width="150" height="113" alt="klematis" />
  46.  
  47. <p><b>Note:</b> In IE, a <!DOCTYPE> must be added for the :hover selector to work on other elements than the <a> element.</p>
  48. </body>
  49. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement