Here is a relatively simple function I've come up with for fading images cross browser:
It does require that you set the relevant style for the image either in the head or external stylesheet (if the beginning opacity is 0), or inline if the beginning opacity is other than 0 (beginning opacity of 0 may also be set inline, if desired). Here is an example for starting with 0 opacity in the head:Code:function fade(el, way, op, opinc, speed){ /* Cross Browser Fader © 2007 John Davenport Scheuer This comment must remain for Legal Use */ clearTimeout(el.timer); if(!fade.t) fade.t=function(o){return typeof el.style[o]=='string'}, fade.ie=el.filters&&el.filters[0], fade.ie6=fade.ie&&typeof el.filters[0].opacity=='number', fade.slctr=fade.t('opacity')||fade.ie6? 'opacity' : fade.t('MozOpacity')? 'MozOpacity' : fade.t('KhtmlOpacity')? 'KhtmlOpacity' : null; var optype=fade.ie6? el.filters[0] : el.style, waym=way=='in'? 1 : -1, speed=speed? speed*1 : 30, opinc=opinc&&opinc>=1? opinc*(fade.ie? 1 : .01) : opinc? opinc : fade.ie? 5 : .05, op=op&&fade.ie? op*1 : op&&op*1>=1? Math.min(op*.01, .99) : op? op : waym>0&&fade.ie? 100 : waym>0? .99 : 0; if(fade.slctr&&Math.abs(op*1-optype[fade.slctr]*1 + opinc*waym)<opinc) optype[fade.slctr]=op; else if(fade.slctr) optype[fade.slctr]=fade.ie6? optype[fade.slctr]*1 + opinc*waym : Math.min(optype[fade.slctr]*1 + opinc*waym, .99); else if (fade.ie&&Math.abs(op*1 - optype.filter.replace(/\D/g,'')*1 + opinc*waym)<opinc) optype.filter='alpha(opacity='+op+')'; else if (fade.ie) optype.filter='alpha(opacity='+[optype.filter.replace(/\D/g,'')*1 + opinc*waym]+')'; else return; if(optype[fade.slctr]&&optype[fade.slctr]*waym<op*waym||!fade.ie6&&fade.ie&&optype.filter.replace(/\D/g,'')*waym<op*waym) el.timer=setTimeout(function(){fade(el, way, op, opinc, speed)}, speed); }
Here is an example for setting the beginning style inline along with example events using the fade function onmouseover/out for an image starting at 50% opacity:Code:<style type="text/css"> #frond img { opacity:0; -moz-opacity:0; -khtml-opacity:0; filter:progid:DXImageTransform.Microsoft.alpha(opacity=0); } </style>
The function itself is versatile and can be used rather simply as shown above or more complexly:HTML Code:<img style="opacity:.50;-moz-opacity:.50;-khtml-opacity:.50;filter:progid:DXImageTransform.Microsoft.alpha(opacity=50);" onmouseover="fade(this, 'in');" onmouseout="fade(this, 'out',50);" src="some.jpg" alt="">
The parameters are:Code:onmouseover="fade(this, 'in',100,2,50);" onmouseout="fade(this, 'out',0,2,50);"
element, 'inORout', target_opacity, opacity_increment, speed
Only the element and 'inOrout' are required. The target opacity defaults to 100 (100%) if not listed and the 'inOrout' is 'in'. If it is 'out' the default is 0. The default increment is 5 degrees of opacity and the default speed is 30 milliseconds between each increment.
This will work in browsers supporting alpha(opacity), opacity, -moz-opacity, or -khtml-opacity, and can be reused as much as required for various images, or the same image on a page. In non-supporting browsers it will do nothing, and since opacity isn't supported in those browsers, the image will simply appear normally.
The function can be used with other elements, except for an unfortunate bug in IE 7 which causes text in any element with any filter to lose its anti-aliasing quality. This used to be able to be overcome (in earlier IE versions) by giving the text a background color, but this is not currently the case in IE 7. Hopefully, MS will fix this bug soon.
Demo:
http://home.comcast.net/~jscheuer1/s...e_advanced.htm



Reply With Quote
The code is pretty nice, but it's virtually unreadable. You might also consider using a closure to define static variables for that function rather than storing them as properties of the function... I find this makes for neater code and eliminates all that if(/* first time we've run */) cruft.
Bookmarks