views:

125

answers:

2

Hello everybody, Once again I find myself stuck by something that I just don't understand. Any help would be appreciated. I'm working on a modal window, you click something and the background is masked and a modal window shows some content. I have a div with "display:none" and "opacity:0", and when the user triggers the modal, this div will overlay everything and have certain transparency to it. In my mind, what I need to do is: Set the opacity Perform a "for" loop that will check if the opacity is less than the desired value. Inside this loop, perform a "setInterval" to gradually increment the value of the opacity until it reaches the desired value. When the desired value has been reached, perform an "if" statement to "clearInterval". My code so far is as follows:

var showMask = document.getElementById('mask');
function fireModal(){
showMask.style.opacity = 0;
showMask.style.display = 'block';
var getCurrentOpacity = showMask.style.opacity;
var increaseOpacity = 0.02;
var finalOpacity = 0.7;
var intervalIncrement = 20;
var timeLapse = 500;
function fadeIn(){
    for(var i = getCurrentOpacity; i < finalOpacity; i++){
        setInterval(function(){
            showMask.style.opacity = i;   
        }, intervalIncrement)    
    }
    if(getCurrentOpacity == finalOpacity){
        clearInterval();
    }
}
fadeIn();
}

As you all can guess, this is not working, all it does is set the opacity to "1" without gradually fade it in. Thanks in advance for your help.

+2  A: 

You should use jquery, mootools or extjs for something like this.

But basically you need to do this:

var id = setInterval(function() {
   showMask.style.opacity += .05;
   if (showMask.style.opacity >= 1)
   {
      clearInterval(id);
   }
},200)

This will fade in over 2 seconds.

Byron Whitlock
Thanks, I'll try this. Unfortunately I can't use jQuery (long story), besides it gives me the chance of learning more raw javascript.
jnkrois
note that id will need to be global or the interval callback won't find it.
Byron Whitlock
I'm not getting any errors, but the behavior is the same, not it changes the opacity to "0.05" immediately, as opposed to in a time lapse.I'll experiment with these a little more so I can get it right, I just want to make sure that my thought process is right.
jnkrois
It should every 1/5 of a seconds, increase the opacity by .05
Byron Whitlock
I'm sure it must be me, it does changes the opacity to "0.05" after 1/5 seconds, but it does it only once and then stops.I'm sure your solution must work, so I will use it and let you know.
jnkrois
Thanks Byron, I was not able to make it work, but I appreciate your help.
jnkrois
A: 

Rack my up as another who strongly advises using jQuery. In my work environment I often face similar challenges due to corporate bosses who are basically afraid of any and all advancement, so I understand your predicament. But, that being said, my suggestion is instead of spending time re-writing the wheel, spend time figuring out how to use the proper solution, which is, in this case jQuery or other javascript framework. If you can write your own function, you can use jQuery.

<script>
  document.write("<scr" + "ipt src='http://givemejquery'&gt;&lt;/scr" + "ipt>");
</script>
Owen Allen
I hear what you are saying and believe me, I agree with you. I had actually written the function already using jQuery, only to realize that the application we are developing is using jQuery 1.1.1 and we can't upgrade.I'd prefer to write plain JavaScript than to use an old (really old) version of jQuery.
jnkrois
Please upgrade your jQuery. If you have version 1.1.1 you can most certainly upgrade it. As a U.I.E. I strongly encourage you to push your company/corporation/client/dictatorship to upgrade and use useful, user-friendly, well maintained tools that help promote a well standardized web. It is our job as developers to protect the web and encourage good practices. Do what it takes to do whats right!
Calvin
I agree with you Calvin, and I already told them that for the sake of efficiency and speed of development, we should upgrade. We will do it in about three months when the rest of the application is ready to be migrated to a new platform. Guess it will be a long wait.
jnkrois