Solved

Dynamically fading table

Posted on 2006-07-05
9
1,215 Views
Last Modified: 2012-06-27
Hello, I use the following to fade the content of a table:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21909311.html

It works well, but it is jumpy.  I want it to be a smooth transition, so that in 1 second, it fades from 100% opacity to 50% opacity.  This needs to be cross browser.

Any ideas?

Thanks!



<style>
.yourClass {
     filter:alpha(opacity=50);
     opacity: 0.50;
     -moz-opacity:0.50;
     }
</style>
<script>
///// thanks pravinasar and 0h4crying0utloud

function fadeTable(tname) {
  document.getElementById(tname).className = "yourClass";
}
</script>

<a href=#" onclick="return fadeTable('myTable');return false">fade table</a>

<table id="myTable" width=550 class="mytableclass" background="http://www.google.com/images/logo.gif"><tr><td height=210>cell 1</td><td>cell 2</td><td>cell 3</td></tr></table>
0
Comment
Question by:hankknight
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:bruno
ID: 17046946
I know they provided you with the fade script, but you might want to look at the Yahoo UI LIbrary animation script, here is their "fade" example":
http://developer.yahoo.com/yui/examples/animation/fade.html

Get more info about the animation utility here:
http://developer.yahoo.com/yui/animation/


bruno  
0
 
LVL 16

Author Comment

by:hankknight
ID: 17046971
Thanks.  That is very interesting, but a bit heavier than I would like.  I would like to see this accomplished with a fraction the amount of code that Yahoo's animation uses.
0
 
LVL 18

Expert Comment

by:bruno
ID: 17047343
ok - but i assume that's more the effect you want?  let me work up some code for you
0
 
LVL 16

Author Comment

by:hankknight
ID: 17047420
Thanks, yes, that is what I am looking for, but I want it as lightweight as possible.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:bruno
ID: 17047422
here's what i have currently, for firefox only - i need to find the js to change the filter value for IE, but this should get you started:



<script>
fade = {
    speed : 5,
    finalOpacity : 30,
    currentOpacity : 100,
    elID : null,
    init : function(el) {
        fade.elID = el;
        if (!document.getElementById(el)) return;
            fadeInterval = setInterval('fade.doFade()',fade.speed);
    },
    doFade : function(obj) {
        obj = document.getElementById(fade.elID);
        if (fade.currentOpacity>fade.finalOpacity) {
            var newOpacity = fade.currentOpacity - 1;
            obj.style.opacity = "."+(newOpacity);
            fade.currentOpacity = newOpacity;
        } else {
            clearInterval(fadeInterval);
        }
    }
}
</script>

<a href=#" onclick="fade.init('myTable');return false">fade table</a>

<table id="myTable" width=550 class="mytableclass" background="http://www.google.com/images/logo.gif"><tr><td height=210>cell 1</td><td>cell 2</td><td>cell 3</td></tr></table>
0
 
LVL 18

Accepted Solution

by:
bruno earned 500 total points
ID: 17047430
easier than i thought it would be to add the IE functionality - here is the entire thing:


<script>
fade = {
    speed : 5,
    finalOpacity : 30,
    currentOpacity : 100,
    elID : null,
    init : function(el) {
        fade.elID = el;
        if (!document.getElementById(el)) return;
            fadeInterval = setInterval('fade.doFade()',fade.speed);
    },
    doFade : function(obj) {
        obj = document.getElementById(fade.elID);
        if (fade.currentOpacity>fade.finalOpacity) {
            var newOpacity = fade.currentOpacity - 1;
            obj.style.opacity = "."+(newOpacity);
            obj.style.filter = "alpha(opacity="+newOpacity+")";
            fade.currentOpacity = newOpacity;
        } else {
            clearInterval(fadeInterval);
        }
    }
}
</script>

<a href=#" onclick="fade.init('myTable');return false">fade table</a>

<table id="myTable" width=550 class="mytableclass" background="http://www.google.com/images/logo.gif"><tr><td height=210>cell 1</td><td>cell 2</td><td>cell 3</td></tr></table>
0
 
LVL 18

Expert Comment

by:bruno
ID: 17047438
btw - i normally like to keep styles seperate from the JS, as was in your original script.  i'd rather change the class name than update the styles dynamically.  but in this situation that doesn't work as you'd need at least 50 different classes, one for each opacity level, and that simply isn't worth it.  

so, we've set all your variables at the top of the script so they are easy to locate and change, and none of the actual values are buried anywhere within the script.
0
 
LVL 16

Author Comment

by:hankknight
ID: 17048144
Thanks!  It is perfect.
0
 
LVL 16

Author Comment

by:hankknight
ID: 17048489
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now