Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic arrows on menu accordion 1

Status
Not open for further replies.

TribeMan

Programmer
Nov 16, 2009
22
IL
I'm a new kid on the Javascript block, and desperately need some help with the following:

I have a small accordion menu in javascript (see below). I wish to add dynamic arrows to each accordion title element, so that an arrowUp or arrowDown image will display depending on whether the accordion element is open or closed.

I believe this will require changes in the js, the css and the html. One of the problems I face is that I am already using a background image for the accordion title, and now want to add a second (arrowUp/arrowDown) image. This requires (I think) a new <div> tag, but the new tag creates errors.

Many thanks to all the bigger kids out there who might help.


===========================
<script type="text/javascript">

var ContentHeight = 200;
var TimeToSlide = 250.0;

var openAccordion = '';

function runAccordion(index)
{
var nID = "Accordion" + index + "Content";
if(openAccordion == nID)
nID = '';

setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
+ openAccordion + "','" + nID + "')", 33);

openAccordion = nID;
}

function animate(lastTick, timeLeft, closingId, openingId)
{
var curTick = new Date().getTime();
var elapsedTicks = curTick - lastTick;

var opening = (openingId == '') ? null : document.getElementById(openingId);
var closing = (closingId == '') ? null : document.getElementById(closingId);

if(timeLeft <= elapsedTicks)
{
if(opening != null)
opening.style.height = ContentHeight + 'px';

if(closing != null)
{
closing.style.display = 'none';
closing.style.height = '0px';
}
return;
}

timeLeft -= elapsedTicks;
var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);

if(opening != null)
{
if(opening.style.display != 'block')
opening.style.display = 'block';
opening.style.height = (ContentHeight - newClosedHeight) + 'px';
}

if(closing != null)
closing.style.height = newClosedHeight + 'px';

setTimeout("animate(" + curTick + "," + timeLeft + ",'"
+ closingId + "','" + openingId + "')", 33);
}
</script>

<style type='text/css'>
.AccordionTitle, .AccordionContent, .AccordionContainer
{
position: relative;
width: 300px; /*changeble*/
}
.AccordionTitle
{
height: 20px; /*changeble*/
overflow: hidden;
cursor: pointer;
font-family: Verdana; /*changeble*/
font-size: 12px; /*changeble*/
font-weight: normal; /*changeble*/
vertical-align: middle; /*changeble*/
text-align: center; /*changeble*/
display: table-cell;
-moz-user-select: none;
border-top: none; /*changeble*/
border-bottom: none; /*changeble*/
border-left: none; /*changeble*/
border-right: none; /*changeble*/
background-color: Green;
color: White;
}
.AccordionContent
{
height: 0px;
overflow: hidden; /*display: none; */
}
.AccordionContent_
{
height: auto;
}
.AccordionContainer
{
border-top: solid 1px #C1C1C1; /*changeble*/
border-bottom: solid 1px #C1C1C1; /*changeble*/
border-left: solid 1px #C1C1C1; /*changeble*/
border-right: solid 1px #C1C1C1; /*changeble*/
}
.ContentTable
{
width: 100%;
text-align: center;
color: White;
}
.ContentCell
{
background-color: #666666;
}
.ContentTable a:link, a:visited
{
color: White;
text-decoration: none;
}
.ContentTable a:hover
{
color: Yellow;
text-decoration: none;
}
</style>

<div id="AccordionContainer" class="AccordionContainer">

<div onclick="runAccordion(1);">
<div class="AccordionTitle" onselectstart="return false;">
Accordion 1
</div>
</div>
<div id="Accordion1Content" class="AccordionContent">
I Am Accordion 1.
</div>

<div onclick="runAccordion(2);">
<div class="AccordionTitle" onselectstart="return false;">
Accordion 2
</div>
</div>
<div id="Accordion2Content" class="AccordionContent">
I Am Accordion 2.
</div>

<div onclick="runAccordion(3);">
<div class="AccordionTitle" onselectstart="return false;">
Accordion 3
</div>
</div>
<div id="Accordion3Content" class="AccordionContent">
I Am Accordion 3.
</div>

<div onclick="runAccordion(4);">
<div class="AccordionTitle" onselectstart="return false;">
Accordion 4
</div>
</div>
<div id="Accordion4Content" class="AccordionContent">
I Am Accordion 4.
</div>

</div>

 
Hi

One possible way :
Code:
[b]function[/b] [COLOR=darkgoldenrod]runAccordion[/color][teal]([/teal]index[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] nID [teal]=[/teal] [green][i]"Accordion"[/i][/green] [teal]+[/teal] index [teal]+[/teal] [green][i]"Content"[/i][/green][teal];[/teal]
  [b]if[/b][teal]([/teal]openAccordion [teal]==[/teal] nID[teal])[/teal]
    nID [teal]=[/teal] [green][i]''[/i][/green][teal];[/teal]

  [highlight][b]with[/b] [teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'dir'[/i][/green][teal]+[/teal]index[teal]))[/teal] [teal]{[/teal][/highlight]
    [highlight]src[teal]=[/teal][green][i]'up.png'[/i][/green][/highlight]
    [highlight]alt[teal]=[/teal]String[teal].[/teal][COLOR=darkgoldenrod]fromCharCode[/color][teal]([/teal][purple]8593[/purple][teal])[/teal][/highlight]
  [highlight][teal]}[/teal][/highlight]

  [highlight][b]if[/b] [teal]([/teal]openAccordion[teal])[/teal] [b]with[/b] [teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'dir'[/i][/green][teal]+[/teal]openAccordion[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\D/g[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])))[/teal] [teal]{[/teal][/highlight]
    [highlight]src[teal]=[/teal][green][i]'down.png'[/i][/green][/highlight]
    [highlight]alt[teal]=[/teal]String[teal].[/teal][COLOR=darkgoldenrod]fromCharCode[/color][teal]([/teal][purple]8595[/purple][teal])[/teal][/highlight]
  [highlight][teal]}[/teal][/highlight]

  [COLOR=darkgoldenrod]setTimeout[/color][teal]([/teal][green][i]"animate("[/i][/green] [teal]+[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]().[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()[/teal] [teal]+[/teal] [green][i]","[/i][/green] [teal]+[/teal] TimeToSlide [teal]+[/teal] [green][i]",'"[/i][/green]
      [teal]+[/teal] openAccordion [teal]+[/teal] [green][i]"','"[/i][/green] [teal]+[/teal] nID [teal]+[/teal] [green][i]"')"[/i][/green][teal],[/teal] [purple]33[/purple][teal]);[/teal]
  
  openAccordion [teal]=[/teal] nID[teal];[/teal]
[teal]}[/teal]
Code:
  [b]<div[/b] [maroon]onclick[/maroon][teal]=[/teal][green][i]"runAccordion(1);"[/i][/green][b]>[/b]
    [b]<div[/b] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionTitle"[/i][/green] [maroon]onselectstart[/maroon][teal]=[/teal][green][i]"return false;"[/i][/green][b]>[/b]
      Accordion 1 [highlight][b]<img[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"dir1"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"down.png"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"&darr;"[/i][/green][b]>[/b][/highlight]
    [b]</div>[/b]
  [b]</div>[/b]
  [b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"Accordion1Content"[/i][/green] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionContent"[/i][/green][b]>[/b]
    I Am Accordion 1.
  [b]</div>[/b]

  [b]<div[/b] [maroon]onclick[/maroon][teal]=[/teal][green][i]"runAccordion(2);"[/i][/green][b]>[/b]
    [b]<div[/b] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionTitle"[/i][/green] [maroon]onselectstart[/maroon][teal]=[/teal][green][i]"return false;"[/i][/green][b]>[/b]
      Accordion 2 [highlight][b]<img[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"dir2"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"down.png"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"&darr;"[/i][/green][b]>[/b][/highlight]
    [b]</div>[/b]
  [b]</div>[/b]
  [b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"Accordion2Content"[/i][/green] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionContent"[/i][/green][b]>[/b]
    I Am Accordion 2.
  [b]</div>[/b]

  [b]<div[/b] [maroon]onclick[/maroon][teal]=[/teal][green][i]"runAccordion(3);"[/i][/green][b]>[/b]
    [b]<div[/b] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionTitle"[/i][/green] [maroon]onselectstart[/maroon][teal]=[/teal][green][i]"return false;"[/i][/green][b]>[/b]
      Accordion 3 [highlight][b]<img[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"dir3"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"down.png"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"&darr;"[/i][/green][b]>[/b][/highlight]
    [b]</div>[/b]
  [b]</div>[/b]
  [b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"Accordion3Content"[/i][/green] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionContent"[/i][/green][b]>[/b]
    I Am Accordion 3.
  [b]</div>[/b]

  [b]<div[/b] [maroon]onclick[/maroon][teal]=[/teal][green][i]"runAccordion(4);"[/i][/green][b]>[/b]
    [b]<div[/b] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionTitle"[/i][/green] [maroon]onselectstart[/maroon][teal]=[/teal][green][i]"return false;"[/i][/green][b]>[/b]
      Accordion 4 [highlight][b]<img[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"dir4"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"down.png"[/i][/green] [maroon]alt[/maroon][teal]=[/teal][green][i]"&darr;"[/i][/green][b]>[/b][/highlight]
    [b]</div>[/b]
  [b]</div>[/b]
  [b]<div[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"Accordion4Content"[/i][/green] [maroon]class[/maroon][teal]=[/teal][green][i]"AccordionContent"[/i][/green][b]>[/b]
    I Am Accordion 4.
  [b]</div>[/b]

Feherke.
 
Great! That's EXACTLY what I needed!

One small secondary issue:

If I run the function "runAccordion(3)", then the page will load with element '3' on the accordion open. (I know how to bypass the windows.onload problem).

Can one place a hyperlink on a second or third page so that the accordion will open with a specified accordion element open, ie something like this:

On myPage1.asp there is a link:
<a href="accordionPage.asp" plus function 'runAccordion(3)'>

On myPage2.asp there is a link:
<a href="accordionPage.asp" plus function 'runAccordion(2)'>

Many thanks,

Small kid on the block (slowly getting bigger)
 
Hi

You can do it with [tt]search[/tt] :
Code:
window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]if[/b] [teal](![/teal]location[teal].[/teal]search[teal])[/teal] [b]return[/b]
  [b]var[/b] param[teal]=[/teal][teal]{}[/teal]
  [b]var[/b] piece[teal]=[/teal]location[teal].[/teal]search[teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal]).[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][fuchsia]/[&;]/[/fuchsia][teal])[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]piece[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]var[/b] pair[teal]=[/teal]piece[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][fuchsia]/=/[/fuchsia][teal])[/teal]
    param[teal][[/teal]pair[teal].[/teal][COLOR=darkgoldenrod]shift[/color][teal]()]=[/teal]pair[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]'='[/i][/green][teal])[/teal]
  [teal]}[/teal]
  [b]if[/b] [teal]([/teal][green][i]'runAccordion'[/i][/green] [b]in[/b] param[teal])[/teal] [COLOR=darkgoldenrod]runAccordion[/color][teal]([/teal]param[teal][[/teal][green][i]'runAccordion'[/i][/green][teal]])[/teal]
[teal]}[/teal]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp?runAccordion=1"[/i][/green][b]>[/b]go 1[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp?runAccordion=2"[/i][/green][b]>[/b]go 2[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp?runAccordion=3"[/i][/green][b]>[/b]go 3[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp?runAccordion=4"[/i][/green][b]>[/b]go 4[b]</a>[/b]
Or with [tt]hash[/tt] :
Code:
window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]if[/b] [teal]([/teal]location[teal].[/teal]hash[teal])[/teal] [COLOR=darkgoldenrod]runAccordion[/color][teal]([/teal]location[teal].[/teal]hash[teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal]))[/teal]
[teal]}[/teal]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp#1"[/i][/green][b]>[/b]go 1[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp#2"[/i][/green][b]>[/b]go 2[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp#3"[/i][/green][b]>[/b]go 3[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"accordionPage.asp#4"[/i][/green][b]>[/b]go 4[b]</a>[/b]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top