I feel like I’m staring stupidity in the face here, but by golly I just can’t figure it out. The timed method won’t call itself. Anyone got a moment to correct me?
[CODE]
<script type=”text/javascript”>
function fadeInOut(element) {
setTimeout(“fadeInOut(this)”, 1000);
if (element.style.opacity == 1) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0;}
}
</script>
[CODE]
setTimeout("fadeInOut("+element+")",1000)
[/CODE]
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type="text/javascript">
var timer;
var fadeInLock = 0;
var fadeOutLock = 0;
function fadeIn(element) {
if (fadeOutLock == 0) {
fadeInLock = 1;
timer = setTimeout("fadeIn("+element+")", 20);
if (element.style.opacity == 0) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 1;clearTimeout(timer);fadeInLock = 0;}
}
}
function fadeOut(element) {
if (fadeInLock == 0) {
fadeOutLock = 1;
timer = setTimeout("fadeOut("+element+")", 20);
if (element.style.opacity == 1) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0;clearTimeout(timer);fadeOutLock = 0;}
}
}
</script>
</head>
<body>
<div id="div1" style="position:absolute; height:100px; width:100px; top: 10px; left:10px; background-color:#3333FF; opacity:1"
onmouseover="fadeOut(div1)" onmouseout="fadeIn(div1)"/>
</body>
</html>
[/CODE]
[CODE]function fadeOut(element, dur, step, cb){
dur=dur || 100; step=step || .1;
if(!document.timer) element.style.opacity= '1';
var o= Number(element.style.opacity)-step;
element.style.opacity= o;
if(o> 0){
document.timer=
setTimeout(function(){fadeOut(element,dur, step,cb)},dur);
}
else{
document.timer= null;
if(cb) return cb(element,dur,step);
}
}[/CODE]
[CODE]function fadeIn(element, dur, step, cb){
dur=dur || 1000; step=step || .1;
if(!document.timer) element.style.opacity= '0';
var o= Number(element.style.opacity)+step;
element.style.opacity= o;
if(o<1){
document.timer=
setTimeout(function(){fadeIn(element,dur, step,cb)},1000);
}
else{
document.timer= null;
if(cb) return cb(element,dur,step);
}
}[/CODE]
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type="text/javascript">
var timer;
var fadeInLock = 0;
var fadeOutLock = 0;
function fadeIn(ele) {
element = document.getElementById(ele)
if (fadeOutLock == 0) {
fadeInLock = 1;
timer = setTimeout("fadeIn('"+element.id+"')", 20);
if (element.style.opacity == 0) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 1;clearTimeout(timer);fadeInLock = 0;}
}
}
function fadeOut(ele) {
if (fadeInLock == 0) {
element = document.getElementById(ele)
fadeOutLock = 1;
timer = setTimeout("fadeOut('"+element.id+"')", 20);
if (element.style.opacity == 1) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0;clearTimeout(timer);fadeOutLock = 0;}
}
}
</script>
</head>
<body>
<div id="div1" style="position:absolute; height:100px; width:100px; top: 10px; left:10px; background-color:#3333FF; opacity:0"
onmouseover="fadeIn(this.id)" onmouseout="fadeOut(this.id)"/>
</body>
</html>
[/CODE]
[CODE]if(document.addEventListener){
fx={
fadeOut: function(element, op, cb){
if(!fx.timer) op= 1;
op-=.02;
element.style.opacity= op;
if(op> 0){
fx.timer=
setTimeout(function(){fx.fadeOut(element, op, cb)},100);
}
else{
delete fx.timer;
if(cb) return cb(element);
}
},
fadeIn: function(element, op, cb){
if(!fx.timer) op= 0;
op+=.02;
element.style.opacity= op;
if(op<1){
fx.timer=
setTimeout(function(){fx.fadeIn(element, op, cb)},100);
}
else{
delete fx.timer;
if(cb) return cb(element);
}
}
}
}
else if(window.attachEvent){
fx={
fadeOut: function(element, op, cb){
if(!fx.timer) op= 100;
op= op-5;
element.style.filter= 'alpha(opacity='+op+')';
if(op> 0){
fx.timer=
setTimeout(function(){fx.fadeOut(element, op, cb)},200);
}
else{
delete fx.timer;
if(cb) return cb(element);
}
},
fadeIn: function(element, op, cb){
if(!fx.timer) op= 0;
op= op+1;
element.style.filter= 'alpha(opacity='+op+')';
if(op<100){
fx.timer=
setTimeout(function(){fx.fadeIn(element, op, cb)},100);
}
else{
delete fx.timer;
if(cb) return cb(element);
}
}
}
}[/CODE]
Note: versions of IE before 8 need the element to be filtered to have layout, as they call it, with a position or zoom property set, among other choices. [/QUOTE]
[CODE]var anim = {
timer : null,
iD : function(id){return document.getElementById(id)},
getStyle : function(styleType) {
var el = this.el;
var styleVal = (el.currentStyle)
? el.currentStyle[styleType]
: document.defaultView.getComputedStyle (el,null).getPropertyValue(styleType);
return styleVal;
},
fade : function(elem, dir, time, fn){
var o = this;
o.el = elem;
o.opac = parseInt(o.getStyle("opacity"))
o.opac = (o.opac>=0)? o.opac : 1; // If Nan (IE) set to 1.
o.cbFn = fn || null;
o.step = (dir=="in") ? (10/time) : -(10/time);
if (o.timer) { clearInterval(o.timer); }
o.timer = setInterval(o.fadeExec,10);
},
fadeExec : function(){
var o = anim; // reset this;
var elSt = o.el.style;
elSt.opacity = o.opac;
elSt.filter = "alpha(opacity="+(o.opac*100)+")";
o.opac = o.opac+o.step;
if (o.opac <= 0 || o.opac >= 1) {
clearInterval(o.timer); o.el = o.timer = null;
if (o.cbFn) { o.cbFn(); }
return o;
}
}
} [/CODE]
[code=php]This a full example with a simple box
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FadeInOut</title>
<style type="text/css">
#box {
position: absolute;
top: 200px; left: 200px;
width: 200px; height: 200px;
background: red;
}
</style>
<script type="text/javascript">
var anim = {
timer : null,
iD : function(id){return document.getElementById(id)},
getStyle : function(styleType) {
var el = this.el;
var styleVal = (el.currentStyle)
? el.currentStyle[styleType]
: document.defaultView.getComputedStyle (el,null).getPropertyValue(styleType);
return styleVal;
},
fade : function(elem, dir, time, fn){
var o = this;
o.el = elem;
o.opac = parseInt(o.getStyle("opacity"))
o.opac = (o.opac>=0)? o.opac : 1; // If Nan (IE) set to 1.
o.cbFn = fn || null;
o.step = (dir=="in") ? (10/time) : -(10/time);
if (o.timer) { clearInterval(o.timer); }
o.timer = setInterval(o.fadeExec,10);
},
fadeExec : function(){
var o = anim; // reset this;
var elSt = o.el.style;
elSt.opacity = o.opac;
elSt.filter = "alpha(opacity="+(o.opac*100)+")";
o.opac = o.opac+o.step;
if (o.opac <= 0 || o.opac >= 1) {
clearInterval(o.timer); o.el = o.timer = null;
if (o.cbFn) { o.cbFn(); }
return o;
}
}
}
function init(){
var box = anim.iD('box');
anim.fade(box, "out", 2000, function(){anim.fade(box, "in", 2000)}); //
}
window.onload = init;
</script>
</head>
<body>
<div id="box">Box</div>
</body>
</html>[/code]
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type="text/javascript">
var timer;
var fadeInLock = 0;
var fadeOutLock = 0;
var methodElement
function fadeIn(element) {
if (fadeOutLock == 0) {
fadeInLock = 1;
methodElement = element;
timer = setTimeout("fadeIn(methodElement)", 20);
if (element.style.opacity == 0) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 1;clearTimeout(timer);fadeInLock = 0;}
}
}
function fadeOut(element) {
if (fadeInLock == 0) {
fadeOutLock = 1;
methodElement = element;
timer = setTimeout("fadeOut(methodElement)", 20);
if (element.style.opacity == 1) {element.style.opacity = 0.9;}
else if (element.style.opacity == 0.9) {element.style.opacity = 0.8;}
else if (element.style.opacity == 0.8) {element.style.opacity = 0.7;}
else if (element.style.opacity == 0.7) {element.style.opacity = 0.6;}
else if (element.style.opacity == 0.6) {element.style.opacity = 0.5;}
else if (element.style.opacity == 0.5) {element.style.opacity = 0.4;}
else if (element.style.opacity == 0.4) {element.style.opacity = 0.3;}
else if (element.style.opacity == 0.3) {element.style.opacity = 0.2;}
else if (element.style.opacity == 0.2) {element.style.opacity = 0.1;}
else if (element.style.opacity == 0.1) {element.style.opacity = 0;clearTimeout(timer);fadeOutLock = 0;}
}
}
</script>
</head>
<body>
<div id="div1" style="position:absolute; height:100px; width:100px; top: 10px; left:10px; background-color:#3333FF; opacity:1"
onmouseover="fadeOut(this)" onmouseout="fadeIn(this)"/>
</body>
</html>
[/CODE]
I'm more intrigued by your routines though. I didn't know how to reference the current value of a style. Cheers for the help! [/QUOTE]
[CODE] function getStyle(element, property) {
el = document.getElementById(element);
if (el.currentStyle){ // Is it Internet Explorer
return el.currentStyle[property];
} else if (window.getComputedStyle) { //mozilla
return document.defaultView.getComputedStyle(el, null)[property];
}
}[/CODE]
[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Default</title>
<style type="text/css">
#box {
position: absolute;
left:100px; top:100px;
width: 200px; height: 200px;
background: red;
}
</style>
<script type="text/javascript">
function init(){
function getStyle(element, property) {
el = document.getElementById(element);
if (el.currentStyle){ // Is it Internet Explorer
return el.currentStyle[property];
} else if (window.getComputedStyle) { //mozilla
return document.defaultView.getComputedStyle(el, null)[property];
}
}
alert ("Width of box is "+getStyle("box","width")); // width of box is 200px
alert ("The left position of the box is "+getStyle("box","left")); // The left position of the box is 100px
alert ("The colour of the box is "+getStyle("box","backgroundColor"));
// in Mozilla: The colour of the box is rgb(255,0,0)
// in IE: The colour of the box is red
}
window.onload = init;
</script>
</head>
<body>
<div id="box">A box</div>
</body>
</html>[/code]
0.1.9 — BETA 5.6