|
Ever wish hipsters would just go away? It's as easy as a click.
![]() The opacity of an HTML object can be changed with javascript using the opacity style. By default, objects are set to an opacity of 1 (100%): For Internet Explorer we need to add an extra piece of code with the filter style. Setting an element's opacity with the filter style is achieved like this: With the filter style you must enter opacity as a percent instead of a decimal. So to achieve a cross-browser opacity setting for any given element, use both of the above styles in conjunction thusly: In javascript, an element's opacity can be set using the opacity object inside the style object: An element's filter style is set in the same way. Once we have cross-browser solution for setting opacity, we can animate a smooth change of opacity using some javascript with the setTimeout() function. setTimeout() has two parameters. The first calls a function after the timer has waited for a designated length of time, and the second tells the timeout how long to wait (in milliseconds). Here's an example of what a simple element fade-out would look like: Below is the full script that makes Pabst the Hispter disappear above. I works in exactly the same way, except that I've added some extra features to control the speed of the fade, allow for a script to be called when the fade is complete, and a little more automation. I've also added some cross-browser compatibility code (to help Internet Explorer along.) The "Browser Detect" script is available from Quirksmode. Feel free to copy and paste the code in your editor of choice and play around with some of the extra parameters. The entire script: |
Comments for "Attention Surplus Disorder"this is what a comment looks like. thought i'd christen this part for you bro. yer blog is rad, props.
posted by Pabst on 2010-02-19
Thanks.
posted by Nick on 2010-02-19
Add a comment |




No Comments for "Web Tricks: Fade In/Fade Out Elements"
Add a comment