Results 1 to 2 of 2

Thread: Javascript fade in&out (without jquery)

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Javascript fade in&out (without jquery)

    Hello all!

    I currently havae this code -

    Code:
    function fadeout(elem, time) {
    	elem = document.getElementById(elem);
    	var startOpacity = elem.style.opacity || 1;
    	elem.style.opacity = startOpacity;
    
    	(function go() {
    		elem.style.opacity -= startOpacity / (time / 100);
    
    		// for IE
    		elem.style.filter = 'alpha(opacity=' + elem.style.opacity * 100 + ')';
    
    		if(elem.style.opacity > 0) {
    			setTimeout(go, 100);
    		} else {
    			elem.visible = 'false';
    		}
    	})();
    }
    Which I'd like to convert so I also have a fadein() function, but not sure how :\
    Any tips (Yes, I know you can do this with jquery, I'm trying to avoid that).
    Also, is there any way to make the fade smoother?
    keyboard1333
    Last edited by keyboard; 09-13-2012 at 03:01 AM.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function zxcFade(id,t,ms){
     var obj=document.getElementById(id),o=zxcFade['zxc'+id],f;
     if (obj&&!o){
      o=zxcFade['zxc'+id]={ };
     }
     if (o){
      clearTimeout(o.dly);
      f=o.now||0;
      t=typeof(t)=='number'&&t>=0&&t<=100?t:0;
      animate(o,obj,f,t,new Date(),(ms||1000)*Math.abs(f-t)/100+5);
     }
    }
    
    function animate(o,obj,f,t,srt,mS){
     var oop=this,ms=new Date().getTime()-srt,now=(t-f)/mS*ms+f;
     if (isFinite(now)){
      obj.style.filter='alpha(opacity='+now+')';
      obj.style.opacity=obj.style.MozOpacity=obj.style.WebkitOpacity=obj.style.KhtmlOpacity=now/100-.001;
      o.now=now;
     }
     if (ms<mS){
      o.dly=setTimeout(function(){ animate(o,obj,f,t,srt,mS); },10);
     }
    }
    
    
    /*]]>*/
    </script></head>
    
    <body onload="zxcFade('tst');">
    <img id="tst" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" alt="img" onmouseup="zxcFade('tst',0,1000)" />
    <input type="button" name="" value="Fade In"  onmouseup="zxcFade('tst',100,1000);"/>
    <input type="button" name="" value="Fade Out"  onmouseup="zxcFade('tst',0,500);"/>
    
    
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Similar Threads

  1. Help with Fade-in Slideshow and Jquery
    By Jim Weinberg in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 06-03-2012, 11:40 AM
  2. Resolved Ultimate Fade-in slideshow jquery issue
    By snstar2006 in forum Bug reports
    Replies: 0
    Last Post: 05-22-2012, 10:21 PM
  3. Resolved Ultimate Fade-In Slideshow and my jQuery
    By Lynxie in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 04-05-2011, 05:34 PM
  4. Jquery fade in out
    By bluewalrus in forum JavaScript
    Replies: 0
    Last Post: 11-17-2009, 10:05 PM
  5. Jquery Link Color Fade Help
    By wyclef in forum JavaScript
    Replies: 0
    Last Post: 04-06-2009, 05:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •