accordion drop down
by c2webdev
HTML
<div class="panel">
<h2>Read the full Direct Debit Guarantee</h2>
<div class="panelcontent">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</div>
CSS
.panel, .panelcollapsed
{ background: #eee; margin:8em 1em 1em 0; padding: 0 0px 2px; width:75%; border:none; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; clear:both;}
.panel h2, .panelcollapsed h2 { font-size:1.2em; font-weight: normal; margin: 0px; padding: 2px; background: url("../arrow-up.gif") no-repeat right #eee;
-moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; display:block; width:100%;}
.panel h2:hover, .panelcollapsed h2:hover { background-color: #eee; font-size:1.2em; display:block;}
.panelcollapsed h2 { background: url("../arrow-dn.gif") no-repeat right #eee; border:none; font-size:1.2em; }
.panelcontent { background: #EEE; clear:both; width:100%; padding:0.1em; border:1px solid #eee;}
.panelcollapsed .panelcontent { display: none; }
.upgrade{background:#ccc!important; color:#000!important;}
JavaScript
var PANEL_NORMAL_CLASS = "panel";
var PANEL_COLLAPSED_CLASS = "panelcollapsed";
var PANEL_HEADING_TAG = "h2";
var PANEL_CONTENT_CLASS = "panelcontent";
var PANEL_COOKIE_NAME = "panels";
var PANEL_ANIMATION_DELAY = 20; /*ms*/
var PANEL_ANIMATION_STEPS = 10;
function setUpPanels()
{
loadSettings();
// get all headings
var headingTags = document.getElementsByTagName(PANEL_HEADING_TAG);
// go through all tags
for (var i=0; i<headingTags.length; i++)
{
var el = headingTags[i];
// make sure it's the heading inside a panel
if (el.parentNode.className != PANEL_NORMAL_CLASS && el.parentNode.className != PANEL_COLLAPSED_CLASS)
continue;
// get the text value of the tag
var name = el.firstChild.nodeValue;
// look for the name in loaded settings, apply the normal/collapsed class
if (panelsStatus[name] == "false")
el.parentNode.className = PANEL_COLLAPSED_CLASS;
else
if (panelsStatus[name] == "true")
el.parentNode.className = PANEL_NORMAL_CLASS;
else
{
// if no saved setting, see the initial setting
panelsStatus[name] = (el.parentNode.className == PANEL_NORMAL_CLASS) ? "true" : "false";
}
// add the click behavor to headings
el.onclick = function()
{
var target = this.parentNode;
var name = this.firstChild.nodeValue;
var collapsed = (target.className == PANEL_COLLAPSED_CLASS);
saveSettings(name, collapsed?"true":"false");
animateTogglePanel(target, collapsed);
};
}
}
/**
* Start the expand/collapse animation of the panel
* @param panel reference to the panel div
*/
function animateTogglePanel(panel, expanding)
{
// find the .panelcontent div
var elements = panel.getElementsByTagName("div");
var panelContent = null;
for (var i=0; i<elements.length; i++)
{
if (elements[i].className == PANEL_CONTENT_CLASS)
{
panelContent = elements[i];
break;
}
}
// make sure the content is visible before getting its height
panelContent.style.display =...