|
|
|
|
|
|
|
|
|
|
|
|
$("#table-1").tableDnD();
quickFlip()
function can now be chained through any jQuery selector.fade
function that accepts a DOM element and a time as parameters:function fade( elem, time )
elem
, the given element. This is the value we have to reduce to 0 in time
, the given number of milliseconds.time
milliseconds, elem
has an opacity of 0.time
/ 100. This is because the loop will go through time
/ 100 iterations in time
milliseconds. In each iteration, the opacity must be decremented by the starting opacity / ( time
/ 100 ) to ensure that it will end up as 0 (the total decrement will equal the starting opacity) after the loop finishes its execution.function fade( elem, time )
{
var startOpacity = elem.style.opacity || 1;
elem.style.opacity = startOpacity;
(function go() {
elem.style.opacity -= startOpacity / ( time / 100 );
setTimeout( go, 100 );
})();
}
Note that startOpacity
is assigned to 1 if elem.style.opacity
is not explicitly set. This explains why we have to set elem.style.opacity
in the following line, as it may not be defined.go
function is recursively called as per our specifications. In the code’s current state, however, it does not stop. To determine when it should finish executing, we have to check whether the element has faded. This can be done by monitoring its opacity in relation to the target value, or 0:if( elem.style.opacity > 0 )
setTimeout( go, 100 );
else
elem.style.display = 'none';
Not only do we stop calling go
when we’re done, but we also set the display to none so that other elements are repositioned; elem
should not continue to take up page space after it has faded. This is the typical functionality of JavaScript libraries and applies to our code as well.elem.style.filter = 'alpha(opacity=' + elem.style.opacity * 100 + ')';
The final code is included below:function fade( elem, time )
{
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.style.display = 'none';
})();
}
And that’s it! See you next time.