www.webdeveloper.com
Results 1 to 5 of 5

Thread: Fade in table background image

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Fade in table background image

    I am trying to get the folowing background image to have a slight fade-in;

    <table width="832" border="0" cellpadding="0" cellspacing="0" background="image02.jpg">

    I have tried various JS samples I have found online but with no success. Seems like this would be an easy thing to do.

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Do you want this to fade in on page load or at a certain time or after a specific event?

    You might want to try something along the lines of:

    Code:
    <!DOCTYPE html>
    
    <style type="text/css">
    
    #test{ background-image: url(someimage.png); opacity: 0;}
    
    </style>
    
    <div id="test" />
    
    <script type="text/javascript">
    
    window.addEventListener("load", fade, false);
    
    // Calls on load.
    function fade()
    {
        var opacity = document.getElementById("test").style.opacity;
        var fade_in = 0;
    
        // Calls an interval every 100 ms.
        fade_in = setInterval(function()
        {
            if(opacity == "" || opacity == "undefined" || opacity == "null")
            {
                // You may have issues incrementing opacity this way. Not sure.
                document.getElementById("test").style.opacity += 0.1;
    
                if(opacity >= 1.0)
                {
                    clearInterval(fade_in);
                    return false;
                }
            }            
        }, 100);
    }
    
    </script>
    This is not tested code so you might have bugs, but this is how I would go about writing a fade function. If you have some code you can post that you'd prefer to use then feel free to show it. This gives me an idea for a tutorial actually, thanks for the idea!

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Thanks for the inspiration. Here's some working code. For the sake of demonstration, I've used this image: http://www.uaegroup.net/uploads/ajax.jpg

    Here's my code, tested in the top 5 browsers with no bugs or console errors.

    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <style type="text/css">
    
    #fade_this
    {
    	background-image: url(someimage.jpg);
    	background-repeat: no-repeat;
    	height: 236px;
    	width: 474px;
    	opacity: 0;
    }
    
    </style>
    
    </head>
    
    <body>
    
    <div id="fade_this"></div>
    
    </body>
    </html>
    
    <script type="text/javascript">
    
    window.addEventListener("load", fade, false);
    
    function fade()
    {
    	var opacity = document.getElementById("fade_this").style.opacity;
    	var fade_in = 0;
    
    	if(typeof fade.counter == "undefined")
    		fade.counter = 0;
    	
    	fade_in = setInterval
    	(
    		function()
    		{
    			// You can adjust this to suit your needs.
    			fade.counter += 0.2;
    			
    			document.getElementById("fade_this").style.opacity = fade.counter;
    			
    			if(document.getElementById("fade_this").style.opacity >= 1)
    			{
    				clearInterval(fade_in);
    				return false;	
    			}
    			
    		}, 50 // 50ms per interval. Changing this speeds up/slows down fade.
    	);
    }
    
    </script>

  4. #4
    Join Date
    Aug 2012
    Posts
    2
    Thanks I will try it out.

  5. #5
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    If you're having trouble, here's my working demo and tutorial explanation:

    Demo: http://www.sitesupplier.co.uk/tutorial_demos/fade/

    Simple tutorial: http://www.sitesupplier.co.uk/index....ith-javascript

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles