Woohoo fading! Everyone loves fading in and out, it is an excellent way to transition between two segments of an application (unless the fade takes too long, and then it just gets annoying). Today we are going to look at a simple and robust way of creating a fade in or fade out effect using javascript and the simple css 'opacity' element.
In the example below, you can see a green box with some text, and a button beneath it. If you press the button, the green box will fade out and become fully transparent, and if you press the button a second time, the box will fade back in. As you can see with the text inside of the box, anything that is a child of the fading element will also fade away/fade back. And, if you've clicked the button enough times, you've probably noticed that if you click it while the box is fading in one direction, it will flip and start fading in the opposite direction.
So how is all of this done? Well, it is actually pretty simple - we are doing an animation across the 'opacity' css element. We have done animation enough times in javascript in various tutorial on the blog that the code below will probably give you a sense of deja-vu. This is because the structure is very similar to some of the other animation code that we have done here, especially Accordion menus. Below, we have the function that kicks off the fade animation. I'll explain what it does after the code:
var TimeToFade = 1000.0;
function fade(eid)
{
var element = document.getElementById(eid);
if(element == null)
return;
if(element.FadeState == null)
{
if(element.style.opacity == null
|| element.style.opacity == ''
|| element.style.opacity == '1')
{
element.FadeState = 2;
}
else
{
element.FadeState = -2;
}
}
if(element.FadeState == 1 || element.FadeState == -1)
{
element.FadeState = element.FadeState == 1 ? -1 : 1;
element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
}
else
{
element.FadeState = element.FadeState == 2 ? -1 : 1;
element.FadeTimeLeft = TimeToFade;
setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
}
}
This fade function takes an element id, and the first thing we do is
resolve it into an element object. The next thing we do is try to
resolve its current 'FadeState', which is a property that this function
will creates on the element when fade is called on that element for
the first time. FadeState is one of 4 numbers - 2 means that the
element is fully opaque, 1 means that the element is currently fading
from transparent to opaque, -1 means that the element is currently
fading from opaque to transparent, and -2 means that the element is
fully transparent. If the FadeState property doesn't exist, we try and
determine it from the state of the opacity css element.
If the FadeState is 1 or -1, it means that an animation is currently
in progress. To flip the animation's direction, we flip the fade state,
and flip the amount of time left to do the animation. So say we had an
element that was fading out (a FadeState of -1), and we had 50
milliseconds left in the animation (out of a total of 500). If fade
was called again on the element at that time, the FadeState would be
flipped to 1 and the time left would be flipped to 450 (i.e., 500 - 50).
Otherwise, if the element is not currently fading in either direction,
we set the fade state appropriately (if its opaque, we want to go to
transparent, and if its transparent we want to go opaque), set the time
to fade (FadeTimeLeft) to the full amount, and call setTimeout on
the fade animation function. And this leads us right into the next chunk
of code, the animateFade function:
function animateFade(lastTick, eid)
{
var curTick = new Date().getTime();
var elapsedTicks = curTick - lastTick;
var element = document.getElementById(eid);
if(element.FadeTimeLeft <= elapsedTicks)
{
element.style.opacity = element.FadeState == 1 ? '1' : '0';
element.style.filter = 'alpha(opacity = '
+ (element.FadeState == 1 ? '100' : '0') + ')';
element.FadeState = element.FadeState == 1 ? 2 : -2;
return;
}
element.FadeTimeLeft -= elapsedTicks;
var newOpVal = element.FadeTimeLeft/TimeToFade;
if(element.FadeState == 1)
newOpVal = 1 - newOpVal;
element.style.opacity = newOpVal;
element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}
As always, the first thing we do inside an animation function is figure out how much time has passed since the animation was last updated. If this amount of time is more than the amount of time left in the animation, we set the element's opacity to the final value (fully transparent if the fade state is -1, and fully opaque if the fade state is 1), and then we return out.
So how do we actually set opacity? Well, for Firefox, Safari, and Opera, it is the CSS element 'opacity'. When 'opacity' is set to 0, the it means fully transparent, and when it is set to '1' it means fully opaque. Of course, IE does something completely different. For opacity in Internet Explorer, you have to set the 'filter' CSS element. And of course, you can't set it with the same values as the opacity element. For IE, "alpha(opacity=0)" is the value to set something transparent, and "alpha(opacity=100)" is how to set something opaque.
Back to the function at hand. Well, if we aren't out of time in the animation, we figure out what percentage opaque we are supposed to be at this current time in the animation. This is accomplished by a simple division of the amount of time left over the total amount of time in the animation. If you are fading out, this is the the new opacity value, and if you are fading in, the new value is 1 minus this figure.
Then, all we need to do is set the new opacity value, and then set
another setTimeout to continue the animation.
I bet your wondering now about the html behind the example above. Well, as you can see below, there is virtually nothing there:
<div id="fadeBlock" style="background-color:Lime;width:250px;
height:65px;text-align:center;">
<br />
I'm Some Text
</div>
<br />
<br />
<input type="button" onclick="fade('fadeBlock');" value="Go" />
The only important part is that the click handler for the button calls
the fade function with the appropriate element id - in this case
'fadeBlock'.
That is it for doing a simple fade in/fade out animation in javascript.
Source Files:
Hi there,
How would you make the box disappear after fading away? (eg. display = none)
Thanks
Use this..
if(element.FadeTimeLeft \<= elapsedTicks) { element.style.opacity = element.FadeState == 1 ? \'1\' : \'0\'; element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')'; element.FadeState = element.FadeState == 1 ? 2 : -2; element.innerHTML = ""; return; }
Dear Sir,
Thanks for the code. But what if I have a set of 5 or more text boxes and want them to appear one after another at an interval of say 20 seconds in a loop.
That is I do not want that the user should press the 'GO' button for the effect to take place, rather it should keep happening on the page automatically
Thanks in advance for the support.
look im flashing!
there is something like you requested vikas but i havent been able to figure out how to get the time changing in there so it says something else after a matter of time. also tell me if u see any flaws in the code ty.
opps lol it read it as code.
the hide function of css might do the trick....
Gr8 work there. but i noticed a bug in IE (counts for 7 and 6) if you remove the height specified as inline style on the faded element it stops the fade any idea why ?
This works great! I tried but couldn't find how to make it so that it will fade to a 50% opacity minimum... could you tell me what change to make because I couldn't figure it out. Thanks!
Sure. You'll need to do two things. First, you need to modify the where the final values are set. Instead of:
You need to say:
Next, to fix what happens during the animation, what you need to do is modify newOpVal after it has been calculated, but before it is used to set the opacity. So instead of this:
You need to say something like:
And now you should be all set.
Thanks you so much! This works great.
I didn't read down the page & basically did the same thing w/ a minor variation.
This part was the same: element.style.opacity = element.FadeState == 1 ? '1' : '0.5'; element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '50') + ')';
And I added this part right below
if(element.FadeState == 1) newOpVal = 1 - newOpVal;
if(element.FadeState == -1 && newOpVal \<= 0.5){ element.FadeState = -2; return; }
I didn't read down the page & basically did the same thing w/ a minor variation.
This part was the same: element.style.opacity = element.FadeState == 1 ? '1' : '0.5'; element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '50') + ')';
And I added this part right below
if(element.FadeState == 1) newOpVal = 1 - newOpVal;
if(element.FadeState == -1 && newOpVal \<= 0.5){ element.FadeState = -2; return; }
Thank you for this! It worked great for a prototype I was doing. I had to modify it as follows:
?1) starts invisible and fades up 2) fades up to .8 and back to 0 3) added switches for style.display (answers JaredC's initial request)
Note that in my case the initial css on the element also needs to be set appropriately. Here's my modified code:
If I were to use this for production code, I might implement variables for the direction and min/max opacity.
Good site guys. I'll be back!
if Peter's code would work i'd appreciate it, i tried replacing everything but it just doesn't work, what i want is something that fades IN on the first try, then fades out when the fade function is used again.
I modified this code in a couple of ways that I personally needed.
?1. Instead of display: none / block (which causes elements around to be shifted whenever the content fades in), I switched it to visibility: hidden / visible.
?2. The script now only fades in, and will not fade out. This is on purpose, because I wanted some content to fade in when the page loaded, but the user could click a link to show it instantly. Previously, if they clicked that link prematurely, the content would fade out when it was normally scheduled to fade in.
If you don't need these changes, then use the other code examples. If you know of a better way to implement it, please share!
Hi Peter, I have found that your code works really well, and I especially like the initial invisibility state of the div.
However there seems to be a little flicker when you first load the div...But once you close it then re-open it, the flicker doesn't happen the second time around...
Any ideas?
really its very good idea for using opacity ....
thanks my problem has solved
Hey, sweet little bit of code. I'm not a programmer by nature;rather a designer. My question is, is it possible to "inject" this code into Spry Sliding panels so they fade instead of sliding? Or, using them on their own, have the current visible block of info fade into the next block of info when a different button is clicked?
Thx in advance either way :P)
It was very handy, Thank you very much!
This is for line
setTimeout("animateFade(" + new Date().getTime() + ",’" + eid + "‘)", 33);
Can I do it without the
new Date().getTime()? just manually giving the milliseconds and incrementing it? ThanksHey, I'm still messing around with the code, but is there a way to set it to 0 on start, then to 100?
I'm trying to set this as something that fades in on click, i have the CSS style set to "display:none" but that kills the whole thing... any ideas how to reverse it?
sorry if it is stated somewhere, i'll re-read this whole page once more also.
Okay, figure out my last issue,
now to read more, unless someone can answer how to use this on multiple id's like i have multiple div's I want to fadein/out but not using the same onclick.. so they are all seperated...
and my quest begins
okay, love the script, figure otu everything i need to know.. but
how do i actually get rid of the divs, 'cause even though transparent, they are taking up space and blocking other things.
any ideas?
Roy,
I am trying to set the text box to 0 on start and make it fade to 100 once you click on it the first time. Can you help me with this?
how can i use a image to fade out and another image to fade in after fadding out the first one on click a button
please advice Mohit
Lets say i want to fade from 40 to 100 and back to 40 ??
NVM i got it .. just put newOpVal += 0.4;
For those who want to remove the element after fading out, just use:
document.body.removeChild(element);
just before the return; in function "animateFade".
Wouldn't another option be to use absolute positioning with a high z-value for the element in question? This would place it on top of other content (while taking it out of the natural document flow). Then, when it faded, your content behind it would be revealed.
i first tried some stuff and changed the code by myself... then i saw that this was already disscussed and copyied it but it was exactly the opposit from what i wanted to get :D
i wanted to fade from 0 to 50 and then back to 0
but now i changed and changed things arround and it doesn't start at 50 it allways goes to full opacity on fadeout... this sucks :D can someone help me out with that problem??
here is the code i had...
I would like to use an imagebutton instead of a normal button, but when I change attribute "type=input" to "type=image", the rectangle disappears and immediately appears.
hey guys, i tweaked the fading a bit. It will start Fading In, then Fading Out - the opposite. I know it was already explained at the top but my tiny little brain got confused and i wasted some time on it. I won't post it here because its a pain in the as* with the quotes and other characters. Here's the link instead. (hope the author dont mind).
Thanks again for all the help! The codes above really helped me :)
great!
so simple but powerful ... :)
Thnx
thank's
Do you know how to use multiple DIV ID's with different buttons/links? For example, I want to use 2 separate links that call this function, and when I click one link it makes one DIV transparent (invisible) before making the second opaque (visible).
Thank you so much for this amazing and useful script! I have just one question please: i have an invisible item ( image... ) and i want to apply the fade in when i make it visible... i'm not good in JS, how can i do that? Thanks a lot
Hi. What attribute can I change/add to delay the initial fade in? I want another action to finish before this one commences. Thanks in advance for any help given.
hi
Can someone post the code to fadeout also. The current code just fades in but not fadeout.
Thanks
Great code! I trying to modify the code so it starts 0% then fades in to 100%
Can someone assist me please.
solved it, Thanks anyway
again great code!
Hi, great code!!! Like "Tabrizz" said: How can you get this to hide the currently visible layer when opening a new one? That would be really useful.
Anyone?
HI nice script man! However, I am wondering how I can customize this like for example I have 3 hidden div's with contents and each has it's own link for toggling it's visibility, so clicking Link1 for example shows Div1 but hides Div2 and 3. Then clicking Link2 show Div2 but hides Div1 and Div3 so on. Of course the default visible is Div1 which the hiding and the showing is controlled by the fading script you have above...
I think this would be a nice application to it, appearing like a pseudo-AJAXy feel to content control...
This is a test
And here is the HTML for the javascript code above:
\ \ \ \ This is a test \ \ \ \ \
This is a great site I spent day's looking all over for this script and the edit(from Shaw) without success just a bunch of nonsense(although it did help my skills being new to JS) Thanks I'll be back
Thanks for the code, really liked the way you resolve it. I'm going to check the rest of the site...
BTW You should check the 'anti-SPAM' on Chrome, I think there something wrong...
Thanks for that nice code! I extended the code to specify min. and max. opacities and made it more compact by merging the two functions 'fade' and 'animateFade' to just one function again called 'fade'! Apart from that it behaves like the original code!
You have to call the function with just one argument, for example: [language] [/language]
Now you can specify different fade settings for each element. Also asynchronous fading is possible. The values in the code are just default values.
Example for fading with default values: [language] [/language] The onclick event resets the fading. This is important when the image is a link. Otherwise the onmouseover and onmouseout events would be inverted after you hit the link and then go back with the browser button!
Example for fading with only max value as default: [language] [/language] The values need to be just specified by the first triggered event.
Example for asynchronous fading: [language] [/language] By fading asynchronous you can't use default values for the fade time!
This is great..But it is not working for IE 8 for me. Can you help. I used the exact code in vio's 03/09/2010 - 08:56 example
the only thing different was how i called the function.
and called the function using this:
i would appreciate if you could show me what to do to make it work in IE 8. Again I used the exact code for the function part. I just copy pasted it. Please send me an email as well if you figured out how to fix this in IE 8. My email is my name :)
ahh the code didn't come up :( but i doubt my calling the function is messing it up for IE 8
This code is amazing! Thank you. Is there a way to direct the page to load with the box faded out, so that clicking triggers a fade in?
Hi, there is nice code on this site.
I used the variant by earlt777@hotmail.com to install it with the 'onload' handler of the body tag.
This worked fine with Firefox/Opera, but it didn't work with IE8.
If I want to fade into a page with IE8, only the following header tags will have success:
Are there experiences to use some variant of the fading script here with body/onload on IE8?
In my last post I forgot the IE8 specific code, here it is:
I would really like this code to fade out the currently visible div on my page when i click any link on the navigation menu, and then load in its place the div containing the content of the clicked link (which should be hidden by default).
Any one got a tweak to make this happen?
Thanks in advance
Can anyone help me with a chat code?
Hello, can any buddy tell my how to get the fade block to be faded on page load?
SpaceProgramer,
I am trying to do the same thing. Did you ever figure out how? Please let me know what I need to do in order to achieve this.
Thanks
Like a few folks have asked so far, does anyone know how to adapt this so that it can be used for multiple DIVs? That is, let's say you had a menu of several links, and for every link there was an associated DIV.
ie: When you click link 1, Div 1 would fade in. Then, when you click link 2, Div 1 would fade out and Div 2 would fade in.
That would be mega-useful and powerful to have. I've made a few stabs at it (I'm a total JS noob), but no luck yet.
Thoughts, ideas or suggestions anyone?
Thanks!
/this is seriously awesome code as it stands though! Thanks!
Hey thanks a lot for this code, its really good. I have somewhat blatantly ripped it off for my website :P so you are mentioned in the source. I edited it a little to make the fadeTime an extra parameter of the methods, so its set when called rather than being the same all the time, for every object.
to postage33 - this code is ideal for calling by other methods. For what you're talking about, I would make a method that calls fade on whatever div that corresponds to the link you clicked, and also calls fade on any divs that are not invisible. This should fade in the clicked div, while fading out any div that is visible, no matter if its fully faded in or not. If you only have one div visible at a time, you just need a variable to keep track of what div was last clicked, so the method knows which other div to call fade on.
Here is my version, i guess i fixe 2 bugs n°1 : fadeIn on an element which has " filter : alpha(opacity=0)" seems doesn't work
n°2 : fadeout on an element which contains elements which are on "position : absolute" seems doesn't work
Ps : sorry for my english... i'm french
Hi Brilliant code - thanks. Can anyone tell me how to modify it to fade only in one direction ie to make to separate functions, one for fade in, the other for fade out, which can be called independently.
I am a total newbie when it comes to JS so if someone could give me the code to make the animation not appear on start but fade to 100 once the button is clicked for the first time, that would be very helpful.
Nice script, I liked it.
I feel you guys can help me get out of the problems below. * element.fadeState, I was able to read and write values to this in firefox. But, it doesn't seem to work in IE.
* I wish to perform animation based on the element's current opacity to specified opacity. For this purpose I have to read the current opacity value, which is quite easy in all browsers except IE.
According the tutorial's text, element.style.opacity works in all browsers except IE. For IE it's element.style.filter.
Thanks The Reddest for the quick response.
how to handle htmlelement.property? is there any other method to do it.
I am not a coder. I have read through and don't see what I am looking for. I have this java script (seen below) and would like to add the fade effect to it. Any suggestions on how to do that?
There are two things I didn't like about the way you implemented this code: the fact you are storing a foreign attribute directly on the element and that you recurse using spliced-in variables. I wanted something more object-oriented without needing to install a framework.
But I otherwise thought the code had potential, so I'm offering my re-working back to everyone else. Note that the class-based approach also easily allows you to fade multiple elements at a time.
Forgot to demonstrate that you could set your single or multiple elements to fade in by passing a 1. (Only really need two states, and fade out is the default state.)
Omit the 1, as in the very last example in my previous post, to fade out.
One last post with apologies: to make this work in IE, you will need to enclose the conditions in an extra set of parentheses.
If you prefer, just copy and paste. See where in my first post the comments indicate what the "helper" function does? Then see the "if" block immediately below that? Replace that "if" block with the following "if" (the top two lines inside the "if" statement contain the enclosing parentheses demanded by IE).
Also, if you want to fade in (as per my second follow-up post), you'll need to add in "state" to your initializer function--I failed to add it to the initializer example. Just replace it with the following:
Last, just a thought. Someone posted an apparently asynchronous solution above that allowed multiple elements to both fade in and out at the same time. There is absolutely no reason in the world you could not set all three things asynchronously--the element, the duration, and its state of fading in or fading out--with this class-based method.
Here's an example--if you want to try this approach, this is the initializer method all over again, this time taking advantage of multiple arrays:
Anyway. :)
Great script, Thanks Mrrena
hi
i like to cover the contents of my page
the script works but the links dont work
how to fix?
chico.woelmuis.nl/erasure/erasure.htm
thnx
Great stuff.
Similar to what Vikas asked earlier and wasn't answered satisfactory yet, I have about 5 texts that should fade in and fade out one after another (in the same space) at an interval of about 20 seconds in a loop.
So there will be no buttons or mouse-actions that trigger anything, but it happens all by itself on the page.
Thanks.
How would I set the code so that the image starts off transparent and then becomes opaque after clicking a button?
Dan, see my post, and the view the source code for the link. I simply inverted the: element.FadeState = -2; } else { element.FadeState = 2;
making it:
element.FadeState = 2; } else { element.FadeState = -2;
now it fades in instead of out. But I have that problem I mentioned...
Loving this code! But one snag for me--I want things to be invisible and then fade into view--I can do that by having the fade triggered by body onload, but this makes them appear briefly before fading out when the page loads. I rather these elements not be seen at all until they are triggered by the user.
I've read through the comments and codes above, and can't find any that really do the trick. How do I make these things stay hidden until triggered?
Here's my test page: http://ianmartinphotography.com/test-site/testimonials/prefade-test-01.html
Any help would be great!
You should be able to set the initial opacity of the div element to 0 in the stylesheet or html file. The function would then act to change it.
See if that works.
Hi Great code..Thanks a lot! Can someone help on how i can fade out a link(or just text) for example and then at the same position fade in another link??
I don't usually comment on stuff on the internet. But I felt that this was worth it. THANK YOU SO MUCH FOR THIS!!!!!!!!!!!!!!!!!!!!!!!!!!
great script ! thank you
hello I have a tooltip script as follows:
I'm not very talented, so you will it be possible to add the code needed to include the "fade function and restored me entirely coded please
Thank you in advance Sincerely, William PS: sorry for the language but I'm French
Hi, I'm trying to get my main images to fade and be hyperlinked to specific pages. Could you help me? The page is: http://www.northland.edu/test
Thank you.
doing exactly the same!
want it for an image transition ill post when done
Hi Jess,
finally finished working on it after managing to fix it for older versions of IE, why microsoft cant just use webkit i dont know, would make it so much easier.
first off my div's on the page are like so
with this css
once you got that its onto the javascript
Great script. Thank you!
One issue I am having: in IE 7 & 8, the text in my DIV is coming out fuzzy. Perhaps it has something to do with this using filters, whereas the good browsers don't?
Have you ever seen or heard of this problem, and any idea how to fix it?
This is awesome! Thanks so much. This makes it so much easier for me.
Hi great code, i would just like it to fade in how do i modify the above code to make it only fade in.
Thanks
Hey,
yes it is a great bit of code, just editing it myself for a pic transition so wrote it to just fade in for you.
you just need to change the second function to this
just tells it to always fade in & always finish at 100% opacity you could set this to 80% or something different if needed
Bim
You guys rock - thanks for sharing the knowledge!! :D :D :D
Could someone please tell me how to add a close button to this? I am using this script on a CSS lightbox and have so far had no luck.
Thanks in advance.
fadeToggle p1 fadeToggle p2
This paragraph has a slow, linear fade.
This paragraph has a fast animation.
You're stupid right ?
I mean, you can't be serious if you are comparing a jquery implementation with a full js implementation of fade (from scratch).
Just had to comment on this. All I can say is lol.
Thank you very much for the tutorial/explanation!
I've been trying to create one of those menus that are hidden outside of the screen and show just a little bit, and slide in when hovered. I've been having a tough time doing this, but with your time method it works just fine. My code is written in a clumsy way, so one shouldn't be scared of an occasional bug. Anyway, here's to see how useful this method can be:
I just set the showMenu() and hideMenu() functions in the onMouseOver and onMouseOut of the menu element.
Thanks again!
I used the example above and changed it to make one string of text disappear and another reappear when clicking on a button. This worked fine. But then I put a background image on the page and now when it fades in and out, it is choppy, there is no longer a smooth fade. Does anyone know why this should be so? Its not like I am reloading the background image with each fade which might cause an extra load on the system...
really great work.....
Hi there, have you ever try this in apex? =)
Thanks and Godspeed! shynn
Hi, just wanted to say thanks for the code. It's helped me out a lot and I only have one question. I'm new to Js and Html (self taught for the past 18 months) and I'm wondering is it possible to make it so it doesn't fade out completely. Say I wanted it to stop so it's only around 30 or 40 percent transparent, do I tell it that once it reaches a predefined value to stop or am I way off the mark???
Cheers for your help (& the code).
Guys with this code how can you just make it juast fade in and not fade out I mean the ID just keeps fading in when clicked ...
I would like to set a timeout that every 5 seconds a new image will fadein, when this image fadesout the same time a fadein will occur with the next image, is this possible??
Great script, solved all my problems, many thanks!
May I ask the whole code? :)
Really Impressive stuff. It took me awhil to understand as I have only begun to learn javascript about two weeks ago, but I have managed to pull alot out of this and am using it to create an image fader that is time based.
Sorry for the big post, but I'm having problems with this code that I based, loosely, off the ideas in this tutorial. It is supposed to be an image fader, that fades an image out, then fades the next one in. It doesn't seem to do anything, even though i have tried activating it in many ways.
Thanks a lot! works perfectly!!
Hi.
I implemented this code to my page. I have some buttons such as "contact" "about us" etc. which I used this fade javascript on to make information appear fading up when I click on one of the buttons.
! My question is...
is there a way to make a automatic fade-out when I click another button...?
hellp thx for the nice code but i try to get the reverse state, that its starts hidden and if u press on the button it appears.
the code i found in the comments doesn´t work :/
have somebody an idea?
Thanks for this very useful and powerful tutorial. I would just like to add one thing I came across while modifying this code. I was trying to wrap the whole script in an anonymous function so I could have multiple objects fading on the page without the variables interfering with each other. When I did this, I was getting errors about undefined functions within setTimeout(). Here is an example of the original:
And here was my solution:
The issue was passing the animateFade function as a string to setTimeout. If setTimeout gets a string, it attempts to evaluate, instead of referencing the actual function. Why it works before wrapping this entire script in an anonymous function I'm not sure (perhaps something to do with function scope), but I thought I would pass along my fix if anyone else was running into this.
See this thread for more info:
http://stackoverflow.com/questions/1101668/how-to-use-settimeout-to-invoke-object-itself
This is just what I'm after - thanks!, just one thing though.. rather than a button, I'm trying to use expand and collapse images that replace each other when the text is displayed/hidden. Anyone any idea how to change the script to do that?
any reason you might be using this in 2012 instead of jquery, mootools, prototype and God knows what all ?
Yeah it is very simple why you would not want to use jQuery. When you have to implement the same piece of code across over 100 sites in your network. Using pure JS is the only way to avoid conflicts.
You learn more if you do it yourself, and you are more able to edit it to have exactly the behavior you want if you are using the effect within a larger script.
hi ! i hope u want to help me, i have a photo in the center of my web page, i want that effect in my photo for some kind of intro effect, please
This does work great for ids but is there anyway to set it to pull a class instead?
I am trying to have an image map that has multiple hover states and displays the contents of a div depending upon where you are on the map. I can get the function to display the contents of the div just fine but I want the div to fade in/out instead of just popping into existence. The function I am currently using uses the getElementById as well and I need the divs to have specific ids.
When I try to insert the [language]onMouseOver="fade(this.id);"[/language] on my image map link it just stops everything from working.
here is the code I'm working with: [language]
This is the seattle information and images
This is the new york information and images
This is some information and images/*this works perfectly fine*/
[/language]
javascript for hide function
Javascript for the fade function.
Thanks for any help you can provide.
Try this to get a list of elements by class:
Then you can give each element pulled by class an id if you want to work with them individually. If you build this id list dynamically at run time and then use it with the fade script you won't have to worry about assigning / maintaining all your id's in the script whenever you change the html of the page.
Hope that helps.
Please help!
I would like to make a simple fading animation of gifs in CSS that then fades to the next page.
I would also like it to be compatible on iphones and ipads.
What is the code needed for this?
Thanks!