views:

55

answers:

2

Hello, someone already has already posted the solution to my question, seen below. I want the same effect but only for certain parts of the page rather than all of the content. I basically only want certain parts of the page to fade in when the mouse is moved.

// attach event handler
document.body.onmousemove = function(){
  fadeIn( this, 1000 );      // 1000ms -> 1s
  this.onmousemove = null; // remove to only fade in once!
};

// sets the opacity of an element (x-browser)
function setOpacity( obj, value ) {
  if ( obj ) {
    obj.style.opacity = value / 100;
    obj.style.filter  = 'alpha(opacity=' + value + ')';
    obj.style.zoom    = 1;
  }
}

// makes an element to fade in
function fadeIn( dom, interval, delay ) {

      interval  = interval || 1000;
      delay     = delay    || 10;

  var opacity   = 0,
      start     = Number(new Date()),
      op_per_ms =  100 / interval;

  if ( typeof dom === "string" ) {
    dom = document.getElementById( dom );
  }

  function step() {

    var now     = Number(new Date()),
        elapsed = now - start;
        opacity = elapsed * op_per_ms;

    setOpacity( dom, opacity );

    if ( elapsed < interval )
      setTimeout( step, delay );
    else
      setOpacity( dom, 100 );
  }

  setTimeout( step, delay );
};
A: 

This is not very difficult using the well-commented code you already have. All you need is to replace the fadeIn() call with more appropriate calls. Pass in either the ID of an element you want to fade or the element itself into the first argument. You can do this for many elements just by calling fadeIn() many times. The animation should occur simultaneously.

Update: In the OP's case, he or she will need to remove the 0 opacity style on the body tag in order for these changes to work.

TNi
This should basically be it. However, it's not working on jsbin. =/
George Marian
The OP will need to remove the 0 opacity style on body as well. Updated answer to reflect this. Edit: Also, the originally faded out elements should have opacity 0 added in.
TNi
Sounds good, thanks.
OIC, I should've noticed that. /headdesk
George Marian
A: 

Instead of fadeIn(this, 1000);

  • Assign a class fade to the elements you want to fade.

  • Add style attribute style="opacity: 0;" to those elements instead of the body element.

  • Get the elements you want to fade

    var elements = document.getElementsByClassName('fade');
    
  • Iterate through the elements fading each one

    for (var i = 0; i < elements.length(); i++) {
        fadeIn(elements[i], 1000);
    }
    

Here's a modified jsbin.

melhosseiny
It should be `document.getElementsByClassName()`, but I would hesitate on using this, since only the newest browsers support it. I am also not sure the OP would want to null the onmousemove of all those elements. Otherwise, this method will work.
TNi
Thanks a lot guys!!
There are lots of cross-browser implementations of this method available, one is http://robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/.
melhosseiny
@melhosseiny: That's absolutely right. I should have made my comment more clear. It was meant to caution the OP to use those cross-browser implementations if he or she uses this method.
TNi
@mellowfellow see http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work.
melhosseiny
Thank you TNi and melhosseiny!
@mellowfellow Use this to get it work cross-browser: http://www.dustindiaz.com/getelementsbyclass/
galambalazs