Forum Moderators: open

Message Too Old, No Replies

Fade in a div

         

andrewsmd

12:02 am on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an error div that I need to hide initially. Based on some js validation, I want to show the error div with a message but I would like to fade it in. I have been trying to do this unsuccessfully for the last two hours. I have googled everything I can think of and can't find anything that will help me do what I need to. Does anyone have some links or suggestions on how to do this?

rainborick

1:35 am on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to gradually increase the CSS opacity of the DIV you want to fade in. If you browse around for slideshow scripts, you'll find some examples of how it's done. It's not difficult, you just have to account for the difference in the syntax for opacity between Internet Explorer and most other browsers. Then use a timing loop or setTimeout() to repeatedly update the setting on the DIV. Good luck!

andrewsmd

5:06 pm on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's the problem. I have tried multiple scripts, but I cannot seem to find anything that is cross browser compatible. Do you have any further suggestions?

Fotiman

5:10 pm on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How about jQuery's fadeIn method:
[api.jquery.com...]

andrewsmd

6:08 pm on Dec 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's exactly what I came across. I couldn't seem to understand it last night but I just needed the good ol get away from it and come back in the morning. Here is the simplest script I could write to accomplish what I wanted to do. I hope this saves someone some time. It works in IE and FF
<!DOCTYPE html>
<html>
<head>
<style>
p { position:relative; width:400px; height:90px; }
#me { position:absolute; width:400px; height:65px;
font-size:36px; text-align:center;
color:yellow; background:red;
padding-top:25px;
top:0; left:0; display:none; }
#you { position:absolute; width:400px; height:65px;
font-size:36px; text-align:center;
color:yellow; background:red;
padding-top:25px;
top:0; left:0; display:none; }
span { display:none; }
</style>
<script src="jquery.js"></script>
</head>
<body>
<p>
Let it be known that the party of the first part
and the party of the second part are henceforth
and hereto directed to assess the allegations
for factual correctness... (<a href="#">click!</a>)
<div id="me"><span>CENSORED!</span></div>

</p><br /><br />
<a href="#" onclick="myFadeIn('#me')">blah</a>
<a href="#" onclick="myFadeOut('#me')">fade</a>
<br /><br />
<div id="you">test test test</div>

<script>
function myFadeIn(id) {
$(id).fadeIn(1000, function () {
$("span").fadeIn(100);
});
return false;
}//runMe
function myFadeOut(id) {

$(id).fadeOut("slow", function() {
// Animation complete.
});
}//fadeMe
</script>

</body>
</html>