Show and Hide areas gradually with a toggle button
To make sections (DIVs or TDs) of your page expnd into view then collapse again when toggling with a button, there is procedure: So when we call our function (e.g. toggleExpansion) we will send it 4 variables: srcObj = area element ID expandTheWidth = true | false expandTheHeight = true | false collapseOthers = "otherElementID:1:1;anotherElementID:1:1" Also, on loading we want to define some parameters: var sizingObject=new Array(); expandTimer=null; expandInterval=2; expandMaxVelocity=200; expandMaxProportion=0.1; expandProportion=3; collapseMaxVelocity=expandMaxVelocity; collapseProportion=expandProportion; The function that will control what happens when a toggle is pressed, will look something like this: function toggleExpansion(srcObj, expandTheWidth, expandTheHeight, collapseOthers) { src=document.getElementById(srcObj); if (src.style.display=="none") { sizeObject(src,expandTheWidth,expandTheHeight,true); if (collapseOthers!="") { objs=collapseOthers.split(";"); for (i=0;i } }else{ sizeObject(src,expandTheWidth,expandTheHeight,false); } } The sizeObject function adds the target object to the array of element to collapse or expand: function sizeObject(srcObj,expandWidth,expandHeight,expand) { if (expand) { if (expandWidth && expandHeight) srcObj.style.position='absolute'; srcObj.style.visibility='hidden'; srcObj.style.display='block'; srcObj.style.overflow='visible'; srcObj.style.width=srcObj.style.height=''; w=srcObj.offsetWidth; h=srcObj.offsetHeight; }else{ w=srcObj.offsetWidth; h=srcObj.offsetHeight; } cw=dw=w; ch=dh=h; if (expand) { if (expandWidth) cw=1; if (expandHeight) ch=1; maxexp=Math.sqrt(Math.pow((dh-ch>dw-cw)?(dh-ch)*expandMaxProportion:(dw-cw)*expandMaxProportion,2)); }else{ if (expandWidth) dw=0; if (expandHeight) dh=0; maxexp=Math.sqrt(Math.pow((ch-dh>cw-dw)?(ch-dh)*expandMaxProportion:(cw-dw)*expandMaxProportion,2)); } if (maxexp>expandMaxVelocity) maxexp=expandMaxVelocity; if (maxexp<5) maxexp=5; srcObj.style.width=cw+"px"; srcObj.style.height=ch+"px"; srcObj.style.overflow='hidden'; srcObj.style.position=''; srcObj.style.visibility='visible'; o={"object":srcObj, "width":expandWidth, "height":expandHeight, "expand":expand, "currentWidth":cw, "currentHeight":ch, "desiredWidth":dw, "desiredHeight":dh, "maxExpand":maxexp}; sizingObject.push(o); if (expandTimer==null) expandTimer = setTimeout("grow()", expandInterval); } The grow function performs the animation of the target element: function grow() { for(i=sizingObject.length-1;i>=0;i--) { dw=dh=0; if (sizingObject[i]["width"]) dw=(sizingObject[i]["desiredWidth"]-sizingObject[i]["currentWidth"])/expandProportion; if (sizingObject[i]["height"]) dh=(sizingObject[i]["desiredHeight"]-sizingObject[i]["currentHeight"])/expandProportion; if (dw>sizingObject[i]["maxExpand"]) dw=sizingObject[i]["maxExpand"]; if (dw<-sizingObject[i]["maxExpand"]) dw=-sizingObject[i]["maxExpand"]; if (dh>sizingObject[i]["maxExpand"]) dh=sizingObject[i]["maxExpand"]; if (dh<-sizingObject[i]["maxExpand"]) dh=-sizingObject[i]["maxExpand"]; sizingObject[i]["currentWidth"]+=dw; sizingObject[i]["currentHeight"]+=dh; sizingObject[i]["object"].style.width=sizingObject[i]["currentWidth"]+"px"; sizingObject[i]["object"].style.height=sizingObject[i]["currentHeight"]+"px"; if (dw<1 && dw>-1 && dh<1 && dh>-1) { if (sizingObject[i]["expand"]) { sizingObject[i]["object"].style.display="block"; sizingObject[i]["object"].style.overflow='visible'; sizingObject[i]["object"].style.width=sizingObject[i]["object"].style.height=''; }else{ sizingObject[i]["object"].style.display="none";
} sizingObject.splice(i,1); } } if (sizingObject.length>0) { expandTimer = setTimeout("grow()",expandInterval); }else{ expandTimer = null; } } This code is used to operate the menu system on the left of this page and the 'Contact', 'Register' and 'Login' areas to the top.
|