JScript object to manage fading a div in and out

Rate It (1)

Last post 01-17-2006 11:53 PM by worldspawn[]. 0 replies.

Sort Posts:

  • JScript object to manage fading a div in and out

    01-17-2006, 11:53 PM
    • Contributor
      4,447 point Contributor
    • worldspawn[]
    • Member since 01-19-2003, 1:21 PM
    • Melbourne, Australia
    • Posts 942

    The below code when initialised against a div will easily fade in and fade out a div. The code is simple and well contained. Steps is the number of opacity changes to make to go from 0 opacity to 100. Duration is the time in ms betweeen each step. Low duration and high steps make for a smoother fade but is more taxing on resources.

    Also included (coz my code uses it) is an implementation for a javascript version of String.Format (String.format in this case); a wonderful piece of handy functionality.

    function fadeManager(div, steps, duration){
     this.div = div;
     this.steps = steps;
     this.duration = duration;
     this.stepCount = 0;
     this.alphaStep = (1 / steps) * 100;
     this.handle = null;
     div.manager = this;
     
     this.fadeIn = function (){
      ref = this;
      this.div.style.filter = String.format("progid:DXImageTransform.Microsoft.Alpha(opacity={0})", this.alphaStep * this.stepCount);
       
      if (this.stepCount == 0)
       this.initialise();
       
      this.stepCount++;
      
      if (this.handle == null)
       this.handle = window.setInterval(function() { ref.fadeIn(); }, this.duration);
       
      if (!(this.stepCount < this.steps)){
       this.reset();
      }
     };
     
     this.fadeOut = function() {
      ref = this;
      opacity = (this.alphaStep * this.stepCount);
      this.div.style.filter = String.format("progid:DXImageTransform.Microsoft.Alpha(opacity={0})", opacity);
       
      this.stepCount--;
      
      if (this.handle == null)
       this.handle = window.setInterval(function() { ref.fadeOut(); }, this.duration);
       
      if (opacity == 0){
       this.reset();
       this.div.style.display = "none";
      }
     }
     
     this.initialise = function(){
      this.div.style.display = "block";
     }
     
     this.reset = function(){
      if (this.handle != null){
       window.clearInterval(this.handle);
       this.handle = null;
      }
     }
    }

    String.format = function(text){
     if (arguments.length == 0)
      throw "Text argument is required.";
      
     if (arguments.length == 1)
      return text;
     
     formatReg = /\{(\d){1}\}/g;
     formatArgs = arguments;
     
     return text.replace(
       formatReg,
       function(){
        index = parseInt(arguments[1]);
        return formatArgs[index+1];
       }
      )
    }

    I use this in an app to fade in and out some help text. To open it I call a handy func just for code reuses sake.

    function openHelp(id){
     div = document.getElementById(id);
     
     var man = new fadeManager(div, 15, 75);
     man.fadeIn();
    }

    Enjoy!

    -- Sam Critchley

    "Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT

    website design
    internet marketing
Page 1 of 1 (1 items)