WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Combining two functions into one, Guillermo, March 19, 2004
     Re:Combining two functions into one, Guillermo, March 19, 2004

Subject: Re:Combining two functions into one From: Guillermo Date: March 19, 2004

Nevermind, I wrote this message after adding the toggle() function.
It's working fine now.


 

From: Guillermo
Sent: March 19, 2004
Subject: Re:Combining two functions into one

I was asking for a solution on animating divs a few days ago. Someone from this board sent me a behavior that works perfectly (ie only, would be nice to convert it so it's cross-browser) but the are two separate functions: One unfolds the div - unfold()-, another folds it back -fold() -. I need to have a single link, when clicked, it unfolds the div, when clicked back, it folds it. Also, the text for he link wold have to change (ie. Show Me | Hide Me).

This are the functions I would like to combine into one:

function toggle()
{
    if( element.bFolded )
    {
        unfold()
    }
    else
    {
        fold()
    }
}

function fold()
{
    if( element.bFolding ) return
    nH = parseInt( element.offsetHeight )
    nS = nH / steps
    for( var nI = 1; nI < steps; nI++ )
    {
        window.setTimeout( uniqueID + ".setpos( " + ( nH - nI*nS ) + " )", time * nI )
    }
    window.setTimeout( uniqueID + ".setpos( " + 1 + ", true )", time * nI )
    element.bFolding = true
    element.bFolded = true
}

function unfold()
{
    if( element.bFolding ) return
    nH = parseInt( element.scrollHeight )
 nCT = element.clientTop
    nS = nH / steps
 if( nS == 0 )
 {
  //a hack
  //display the element at a distant absolute position to check it's
  //height
  element.runtimeStyle.position = 'absolute'
  element.runtimeStyle.top = '-1000px'
     element.runtimeStyle.display = 'block'

     nH = parseInt( element.scrollHeight )
  nCT = element.clientTop
     nS = nH / steps

  element.runtimeStyle.position = 'relative'
  element.runtimeStyle.top = '0px'
     element.runtimeStyle.display = 'none'
 }

    setpos(1)
    element.runtimeStyle.display = 'block'
    for( var nI = 1; nI < steps; nI++ )
    {
        window.setTimeout( uniqueID + ".setpos( " + ( nI*nS ) + " )", time * nI )
    }
    window.setTimeout( uniqueID + ".setpos( " + ( nH + 2*nCT ) + ", true )", time * nI )
    element.bFolding = true

    element.bFolded = false
}


function setpos( nH, bEnd )
{
    nNH = Math.floor( nH )
    element.runtimeStyle.height = nNH
    if( bEnd )
    {
        if( element.bFolded )
        {
            element.runtimeStyle.display = 'none'
        }
        element.bFolding = false
    }
}

The function is called with: onclick="mydiv.unfold()" or onclick="mydiv.fold()"





Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX