fadeElement Test Page

Hover over the images below to see the effect in action.

Red Square Green Square Blue Square Red Square Green Square Blue Square Red Square Green Square Blue Square Red Square Green Square Blue Square

Code

  1. var fadeElement =
  2. {
  3. initialise: function(obj)
  4. {
  5. // Set initial opacity for the object:
  6. fadeElement.setOpacity(obj, 20);
  7. // Add the object to objArray:
  8. fadeElement.objArray.push(obj);
  9. // Initialise the fadeState property:
  10. obj.fadeState = '';
  11. obj.onmouseover = function() { this.fadeState = 'fadingIn'; }
  12. obj.onmouseout = function() { this.fadeState = 'fadingOut'; }
  13. // Set a timer to call the fader method:
  14. if (!window.fadeTimer) window.fadeTimer = setInterval(fadeElement.fader, 50);
  15. },
  16. setOpacity: function(obj, opacity)
  17. {
  18. obj.style.filter = "alpha(opacity=" + opacity + ")"; // For IE filter to work, obj MUST have layout
  19. obj.style.KHTMLOpacity = opacity / 100; // Safari and Konqueror
  20. obj.style.MozOpacity = opacity / 100; // Old Mozilla and Firefox
  21. obj.style.opacity = opacity / 100; // CSS3 opacity for browsers that support it
  22. },
  23. fader: function()
  24. {
  25. // Loop through all objects in objArray:
  26. for (var i = 0; i < fadeElement.objArray.length; i++)
  27. {
  28. var obj = fadeElement.objArray[i];
  29. var opacity = obj.style.opacity * 100;
  30. // Check if object is animated:
  31. if ((obj.fadeState == 'fadingIn') && opacity < 100)
  32. fadeElement.setOpacity(obj, opacity + 10);
  33. else if ((obj.fadeState == 'fadingOut') && opacity > 20)
  34. fadeElement.setOpacity(obj, opacity - 10);
  35. }
  36. },
  37. objArray: [] // This array stores each object passed to the script.
  38. }

Copyright © 2006 Iain Gardiner (http://www.firelightning.com/)