Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Dynamically fading table

Hello, I use the following to fade the content of a table:
https://www.experts-exchange.com/questions/21909311/Apply-CSS-with-JS-function.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>
Avatar of bruno
bruno
Flag of United States of America image

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  
Avatar of hankknight

ASKER

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.
ok - but i assume that's more the effect you want?  let me work up some code for you
Thanks, yes, that is what I am looking for, but I want it as lightweight as possible.
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>
ASKER CERTIFIED SOLUTION
Avatar of bruno
bruno
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks!  It is perfect.