Solved

Dynamically fading table

Posted on 2006-07-05
9
1,219 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

867 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

14 Experts available now in Live!

Get 1:1 Help Now