/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Fading div revisited – visibility question…

Hi,

I have created a div that will fade out before being made hidden. The strange thing about it is that on the first time around the fading is smooth and fairly slow. But… if one clicks on the button again the speed of fading increases – the more one clicks the faster it fades out.

I have put alerts in to see if the divFade function is working properly (counting the values down for opacity) and it is. I just can’t seem to find the problem and why the rate of fade increase.

Would someone mind taking a look at the following code and see if the problem can be determined?

The code example creates a div, then slowly gets it to fade out at which point it is made hidden. If one clicks on the button again it will make the div visible and then slowly make it fade again.

Any help would be greatly appreciated…

[code]
<head>
<title>Example</title>
<script type=”text/javascript”>

var intl;
var number;

function testFade(){
if(eltNotify=document.getElementById(‘text’)){eltNotify.style.visibility=””;}
else{

var eltBody = document.getElementsByTagName(‘body’).item(0);
eltNotify = document.createElement(‘div’);
eltNotify.id=’text’;
eltNotify.style.display = ‘block’;
eltNotify.innerHTML = ‘This is fading!’;
eltBody.appendChild(eltNotify);
}
eltNotify.style.width=’90’;
eltNotify.style.height=’21’;
eltNotify.style.backgroundColor=’gold’;

if(navigator.userAgent.indexOf(“MSIE”) == -1){number=1;}
else {number=100;}
eltNotify.style.opacity = number;
eltNotify.style.filter = ‘alpha(opacity=’ + number + ‘)’;
int1 = setInterval(“divFade(‘text’);”,75);

}

function divFade(id){
var object = document.getElementById(id);
if(number>0){
if(navigator.userAgent.indexOf(“MSIE”) == -1){
object.style.opacity = number;
number-=.05;
number=Math.round(number*100)/100;
} else {
object.style.filter = ‘alpha(opacity=’ + number + ‘)’;
number-=5;
}
}

else {
clearInterval(intl);
object.style.visibility=”hidden”;
}
}

</script>
</head>
<body>
<input type=”button” value=”Test Fade” id=”button” onclick=”testFade()”>
<br><br>
</body>

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@aaronbassettMay 10.2007 — You are not clearing your interval as you use 2 different variable names

int[b]1[/b] = setInterval("divFade('text');",75);

clearInterval(int[b]l[/b]);

change it to:
intl = setInterval("divFade('text');",75);

and it works fine
Copy linkTweet thisAlerts:
@Mike_BurdickauthorMay 10.2007 — aaronbassett,

THANKS!!!

Great catch - for some reason my editor makes the "1" and the "l" look identical:o . I'll remember that, and never use those letters like that again.

Regardless, that was was a difficult find on your part and your help is greatly appreciated - I don't think I'd ever have gotten that!
×

Success!

Help @Mike_Burdick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...