W
windandwaves
Hi Folk
I just wrote a function (see below). Now, it occurred to me that it
is hard to use the same function on a page twice and at the same
time. How would I change the function below into an object / class
that:
- I can use twice at the same time
- the variables don't affect other variables that maybe used on the
page (i.e. take them out the global domain)
Hope that makes sense.
Thank you.
//you can set these variables
var totalTime = 1000;//in milliseconds
var startOpacity = 99.9999; // starting point for fade
var stopOpacity = 0; //end point for fade
var numberOfSteps = 30; //number of steps
//other variables
var speed; //in milliseconds
var opacity; //starting point
var step; //interval for each one
var div;
var startOpacity;
function startFade(elName){
opacity = startOpacity;
step = (stopOpacity - startOpacity) / numberOfSteps;
speed = totalTime / numberOfSteps;
div = document.getElementById(elName);
repeatFade();
return true;
}
function repeatFade() {
opacity += step;
var last = false;
if(Math.abs(stopOpacity - opacity) < Math.abs(step)) {
opacity = stopOpacity;
last = true;
}
if(opacity >= 0 && opacity <= 100) {
div.style.filter = "alpha(opacity="+Math.round(opacity)+")";// IE/
Winfilter: alpha(opacity=50)
div.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
div.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
div.style.opacity = opacity/100;// Safari 1.2, newer Firefox and
Mozilla, CSS3
if(last == false) {
window.setTimeout("repeatFade()", speed);
}
}
}
I just wrote a function (see below). Now, it occurred to me that it
is hard to use the same function on a page twice and at the same
time. How would I change the function below into an object / class
that:
- I can use twice at the same time
- the variables don't affect other variables that maybe used on the
page (i.e. take them out the global domain)
Hope that makes sense.
Thank you.
//you can set these variables
var totalTime = 1000;//in milliseconds
var startOpacity = 99.9999; // starting point for fade
var stopOpacity = 0; //end point for fade
var numberOfSteps = 30; //number of steps
//other variables
var speed; //in milliseconds
var opacity; //starting point
var step; //interval for each one
var div;
var startOpacity;
function startFade(elName){
opacity = startOpacity;
step = (stopOpacity - startOpacity) / numberOfSteps;
speed = totalTime / numberOfSteps;
div = document.getElementById(elName);
repeatFade();
return true;
}
function repeatFade() {
opacity += step;
var last = false;
if(Math.abs(stopOpacity - opacity) < Math.abs(step)) {
opacity = stopOpacity;
last = true;
}
if(opacity >= 0 && opacity <= 100) {
div.style.filter = "alpha(opacity="+Math.round(opacity)+")";// IE/
Winfilter: alpha(opacity=50)
div.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
div.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
div.style.opacity = opacity/100;// Safari 1.2, newer Firefox and
Mozilla, CSS3
if(last == false) {
window.setTimeout("repeatFade()", speed);
}
}
}