PDA

View Full Version : Horizontal Accordion script: Dynamically centered, dynamic peek width



Dex
12-17-2010, 11:26 AM
1) Script Title: Horizontal Accordion script

2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex17/haccordion.htm

3) Describe problem:
How much script modification would be needed to have the horizontal accordion completely dynamic (e.g. dynamic "peek" widths, dynamic "full" widths and dynamically centered)? I understand that a fully dynamic horizontal accordion wouldn't be a trivial task especially since I'm new to javascript. I've been playing with the horizontal accordion script trying to make a menu system similar to this (uses flash): http://www.park-mcdonald.com/

Any tips or pointers on where I should concentrate my efforts? I'd be OK with temporarily hard coding widths until I'm comfortable with the javascript.

Great learning site by the way. Thanks in advance!

vwphillips
12-17-2010, 07:23 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
#tst {
position:relative;left:200px;top:0px;width:220px;height:170px;border:solid black 1px;
}

.nav {
position:relative;left:200px;width:100px;
}


/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/

var img=new Image();
img.src='http://www.vicsjavascripts.org.uk/StdImages/Cartoon1.gif';

/*]]>*/
</script>
<script type="text/javascript">
// Animate (11-January-2010)
// by Vic Phillips http://www.vicsjavascripts.org.uk

// To progressively change the Left, Top, Width, Height or Opacity of an element over a specified period of time.
// With the ability to scale the effect time on specified minimum/maximum values
// and with three types of progression 'sin' and 'cos' and liner.

// **** Application Notes

// **** The HTML Code
//
// when moving an element the inline or class rule style position of the element should be assigned as
// 'position:relative;' or 'position:absolute;'
//
// The element would normally be assigned a unique ID name.
//

// **** Initialising the Script.
//
// The script is initialised by assigning an instance of the script to a variable.
// e.g A = new zxcAnimate('left','id1')
// where:
// A = a global variable (variable)
// parameter 0 = the mode(see Note 1). (string)
// parameter 1 = the unique ID name or element object. (string or element object)
// parameter 1 = the initial value. (digits, default = 0)

// **** Executing the Effect
//
// The effect is executed by an event call to function 'A.animate(10,800 ,5000,[10,800]);'
// where:
// A = the global referencing the script instance. (variable)
// parameter 0 = the start value. (digits, for opacity minimum 0, maximum 100)
// parameter 1 = the finish value. (digits, for opacity minimum 0, maximum 100)
// parameter 2 = period of time between the start and finish of the effect in milliseconds. (digits or defaults to previous or 0(on first call) milliSeconds)
// parameter 3 = (optional) to scale the effect time on a specified minimum/maximum. (array, see Note 3)
// field 0 the minimum value. (digits)
// field 1 the maximum value. (digits)
// parameter 3 = (optional) the type of progression, 'sin', 'cos' or 'liner'. (string, default = 'liner')
// 'sin' progression starts fast and ends slow.
// 'cos' progression starts slow and ends fast.
//
// Note 1: Examples modes: 'left', 'top', 'width', 'height', 'opacity.
// Note 2: The default units(excepting opacity) are 'px'.
// For hyphenated modes, the first character after the hyphen must be upper case, all others lower case.
// Note 3: The scale is of particular use when re-calling the effect
// in mid progression to retain an constant rate of progression.
// Note 4: The current effect value is recorded in A.data[0].
// Note 5: A function may be called on completion of the effect by assigning the function
// to the animator intance property .Complete.
// e.g. [instance].Complete=function(){ alert(this.data[0]); };
//



// **** Functional Code(1.52K) - NO NEED to Change

function zxcAnimate(mde,obj,srt){
this.to=null;
this.obj=typeof(obj)=='object'?obj:document.getElementById(obj);
this.mde=mde.replace(/\W/g,'');
this.data=[srt||0];
return this;
}

zxcAnimate.prototype={

animate:function(srt,fin,ms,scale,c){
clearTimeout(this.to);
this.time=ms||this.time||0;
this.neg=srt<0||fin<0;
this.data=[srt,srt,fin];
this.mS=this.time*(!scale?1:Math.abs((fin-srt)/(scale[1]-scale[0])));
this.c=typeof(c)=='string'?c.charAt(0).toLowerCase():this.c?this.c:'';
this.inc=Math.PI/(2*this.mS);
this.srttime=new Date().getTime();
this.cng();
},

cng:function(){
var oop=this,ms=new Date().getTime()-this.srttime;
this.data[0]=Math.floor(this.c=='s'?(this.data[2]-this.data[1])*Math.sin(this.inc*ms)+this.data[1]:this.c=='c'?this.data[2]-(this.data[2]-this.data[1])*Math.cos(this.inc*ms):(this.data[2]-this.data[1])/this.mS*ms+this.data[1]);
this.apply();
if (ms<this.mS) this.to=setTimeout(function(){oop.cng()},10);
else {
this.data[0]=this.data[2];
this.apply();
if (this.Complete) this.Complete(this);
}
},

apply:function(){
if (isFinite(this.data[0])){
if (this.data[0]<0&&!this.neg) this.data[0]=0;
if (this.mde!='opacity') this.obj.style[this.mde]=this.data[0]+'px';
else zxcOpacity(this.obj,this.data[0]);
}
}

}

function zxcOpacity(obj,opc){
if (opc<0||opc>100) return;
obj.style.filter='alpha(opacity='+opc+')';
obj.style.opacity=obj.style.MozOpacity=obj.style.WebkitOpacity=obj.style.KhtmlOpacity=opc/100-.001;
}


</script>


</head>

<body>
<div id="tst" >
<div class="set" >
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon1.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" alt="Egypt" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon3.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon4.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon5.gif" alt="Cartoon" />
</div>
<div class="set" >
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg" alt="Egypt" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon2.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg" alt="Egypt" />
</div>
</div>

<div id="nav" >
<img id="b1" src="http://www.vicsjavascripts.org.uk/StdImages/Left1.gif" />
<a id="d1" >fffff</a>
<img id="f1" src="http://www.vicsjavascripts.org.uk/StdImages/Right1.gif" />
<div class="swap" >set 1</div>
<div class="swap" >set 2</div>
</div>

<script type="text/javascript">
/*<![CDATA[*/

function Gallery(o){
var obj=document.getElementById(o.ID),panels=this.bycls(o.PanelClassName,obj),swaps,imgs,z0,z0a,z1;
obj.style.overflow='hidden';
this.w=obj.offsetWidth;
this.h=obj.offsetHeight;
this.sets=[];
this.cnts=[];
for (z0=0;z0<panels.length;z0++){
panels[z0].style.position='absolute';
panels[z0].style.top='0px';
panels[z0].style.left='0px';
panels[z0].style.width=this.w+'px';
panels[z0].style.height=this.h+'px';
this.sets[z0]=[];
this.cnts[z0]=0;
imgs=panels[z0].getElementsByTagName('IMG')
for (z0a=0;z0a<imgs.length;z0a++){
imgs[z0a].style.position='absolute';
this.sets[z0][z0a]=new zxcAnimate('opacity',imgs[z0a],z0a>0?0:100);
this.sets[z0][z0a].Complete=function(){
if (this.data[0]==0){
this.obj.style.visibility='hidden';
}
}
}
this.reset(z0);
panels[z0].style.visibility=z0>0?'hidden':'visible';
}
this.panels=panels;
obj=document.getElementById(o.ForwardID);
if (obj){
this.addevt(obj,'mouseup','Animate',1);
}
obj=document.getElementById(o.BackID);
if (obj){
this.addevt(obj,'mouseup','Animate',-1);
}
this.paginate=document.getElementById(o.PaginateID);
obj=document.getElementById(o.SwapID);
if (obj){
swaps=this.bycls(o.SwapClassName,obj)
for (z1=0;z1<swaps.length;z1++){
this.addevt(swaps[z1],'mouseup','Swap',z1);
}
}
this.to=null;
this.set=0;
this.ms=o.AnimateSpeed||1000;
this.Animate(0);
}

Gallery.prototype={

Animate:function(ud){
var cnt=this.cnts[this.set],oop=this.sets[this.set][cnt],lgth=this.sets[this.set].length-1;
oop.animate(oop.data[0],0,this.ms);
cnt+=ud;
cnt=cnt<0?lgth:cnt>lgth?0:cnt;
oop=this.sets[this.set][cnt];
oop.obj.style.left=(this.w-oop.obj.offsetWidth)/2+'px';
oop.obj.style.top=(this.h-oop.obj.offsetHeight)/2+'px';
oop.obj.style.visibility='visible';
oop.animate(oop.data[0],100,this.ms);
this.cnts[this.set]=cnt;
if (this.paginate){
this.paginate.innerHTML=(cnt+1)+' of '+(lgth+1);
}
},

Swap:function(nu){
clearTimeout(this.to);
this.reset(this.set);
this.panels[this.set].style.visibility='hidden';
this.set=nu;
this.panels[this.set].style.visibility='visible';
this.reset(this.set);
this.Animate(0);
},

reset:function(nu){
this.cnts[nu]=0;
var z0,ary=this.sets[nu]
for (z0=0;z0<ary.length;z0++){
ary[z0].obj.style.visibility='hidden';
ary[z0].data[0]=z0>0?0:100;
zxcOpacity(ary[z0].obj,z0>0?0:100);
}
},

addevt:function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](p,e);}, false);
else o.attachEvent('on'+t,function(e){ return oop[f](p,e); });
},

bycls:function (nme,el){
for (var reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName('*'),ary=[],z0=0; z0<els.length;z0++){
if(reg.test(els[z0].className)){
ary.push(els[z0]);
}
}
return ary;
}


}

new Gallery({
ID:'tst',
PanelClassName:'set',
ForwardID:'f1',
BackID:'b1',
PaginateID:'d1',
AnimateSpeed:1000,
SwapID:'nav',
SwapClassName:'swap'
});
/*]]>*/
</script>
</body>

</html>

Dex
12-18-2010, 02:34 AM
I should probably clarify, I'm trying to replicate the animated/sliding horizontal accordion menu at the bottom of http://www.park-mcdonald.com/ (Products, About, Contact). The gallery is helpful however. Thanks :)

vwphillips
12-18-2010, 01:55 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
#panels {
position:relative;left:200px;top:0px;width:320px;height:220px;
}

.panel {
position:relative;left:200px;top:0px;width:320px;height:220px;background-Color:#FFFFCC;
}

#tst {
position:absolute;left:50px;top:10px;width:220px;height:170px;
}

.nav {
position:relative;left:200px;width:100px;
}

.control {
position:absolute;left:120px;top:190px;width:100px;
}

.frames {
position:relative;left:100px;top:0px;width:520px;height:100px;

}

#frames {
position:relative;left:100px;top:0px;width:520px;height:100px;

}
.float {
float:left;
}

.projects {
width:100px;float:left;text-Align:center;
}

#nav {
width:80px;height:60px;float:left;

}

#nav UL{
position:relative;left:-20px;width:50px;height:100px;
}

/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/

var img=new Image();
img.src='http://www.vicsjavascripts.org.uk/StdImages/Cartoon1.gif';

/*]]>*/
</script>
<script language="JavaScript" src="http://www.vicsjavascripts.org.uk/Animate/Animate.js" type="text/javascript"></script>

</head>

<body>

<div id="panels" >

<div class="panel" >
<div id="tst" >
<div class="set" >
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon1.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" alt="Egypt" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon3.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon4.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon5.gif" alt="Cartoon" />
</div>
<div class="set" >
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg" alt="Egypt" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Cartoon2.gif" alt="Cartoon" />
<img src="http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg" alt="Egypt" />
</div>
</div>
<div class="control" >
<img id="b1" src="http://www.vicsjavascripts.org.uk/StdImages/Left1.gif" />
<a id="d1" >fffff</a>
<img id="f1" src="http://www.vicsjavascripts.org.uk/StdImages/Right1.gif" />
</div>
</div>

<div class="panel" style="width:400px;" >
Panel 2
</div>

<div class="panel" >
Panel 3
</div>

</div>

<div class="frames" >

<div id="frames" >

<div class="float" >
<div class="projects" >
Projects
</div>
<div id="nav" class="toggle" >
<table style="width:80px;" >
<tr>
<td class="swap">Set 1</td>
</tr>
<tr>
<td class="swap">Set 2</td>
</tr>
</table>
</div>
</div>

<div class="float" >
<div class="projects" >
About
</div>
</div>

<div class="float" >
<div class="projects" >
Contact
</div>
</div>

</div>

</div>

</div>
<script type="text/javascript">
/*<![CDATA[*/

function Gallery(o){
var obj=document.getElementById(o.ID),panels=this.bycls(o.PanelClassName,obj),swaps,imgs,z0,z0a,z1;
obj.style.overflow='hidden';
this.w=obj.offsetWidth;
this.h=obj.offsetHeight;
this.sets=[];
this.cnts=[];
for (z0=0;z0<panels.length;z0++){
panels[z0].style.position='absolute';
panels[z0].style.top='0px';
panels[z0].style.left='0px';
panels[z0].style.width=this.w+'px';
panels[z0].style.height=this.h+'px';
this.sets[z0]=[];
this.cnts[z0]=0;
imgs=panels[z0].getElementsByTagName('IMG')
for (z0a=0;z0a<imgs.length;z0a++){
imgs[z0a].style.position='absolute';
this.sets[z0][z0a]=new zxcAnimate('opacity',imgs[z0a],z0a>0?0:100);
this.sets[z0][z0a].Complete=function(){
if (this.data[0]==0){
this.obj.style.visibility='hidden';
}
}
}
this.reset(z0);
panels[z0].style.visibility=z0>0?'hidden':'visible';
}
this.panels=panels;
obj=document.getElementById(o.ForwardID);
if (obj){
this.addevt(obj,'mouseup','Animate',1);
}
obj=document.getElementById(o.BackID);
if (obj){
this.addevt(obj,'mouseup','Animate',-1);
}
this.paginate=document.getElementById(o.PaginateID);
obj=document.getElementById(o.SwapID);
if (obj){
swaps=this.bycls(o.SwapClassName,obj)
for (z1=0;z1<swaps.length;z1++){
this.addevt(swaps[z1],'mouseup','Swap',z1);
}
}
this.to=null;
this.set=0;
this.ms=o.AnimateSpeed||1000;
this.Animate(0);
}

Gallery.prototype={

Animate:function(ud){
var cnt=this.cnts[this.set],oop=this.sets[this.set][cnt],lgth=this.sets[this.set].length-1;
oop.animate(oop.data[0],0,this.ms);
cnt+=ud;
cnt=cnt<0?lgth:cnt>lgth?0:cnt;
oop=this.sets[this.set][cnt];
oop.obj.style.left=(this.w-oop.obj.offsetWidth)/2+'px';
oop.obj.style.top=(this.h-oop.obj.offsetHeight)/2+'px';
oop.obj.style.visibility='visible';
oop.animate(oop.data[0],100,this.ms);
this.cnts[this.set]=cnt;
if (this.paginate){
this.paginate.innerHTML=(cnt+1)+' of '+(lgth+1);
}
},

Swap:function(nu){
clearTimeout(this.to);
this.reset(this.set);
this.panels[this.set].style.visibility='hidden';
this.set=nu;
this.panels[this.set].style.visibility='visible';
this.reset(this.set);
this.Animate(0);
},

reset:function(nu){
this.cnts[nu]=0;
var z0,ary=this.sets[nu]
for (z0=0;z0<ary.length;z0++){
ary[z0].obj.style.visibility='hidden';
ary[z0].data[0]=z0>0?0:100;
zxcOpacity(ary[z0].obj,z0>0?0:100);
}
},

addevt:function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](p,e);}, false);
else o.attachEvent('on'+t,function(e){ return oop[f](p,e); });
},

bycls:function (nme,el){
for (var reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName('*'),ary=[],z0=0; z0<els.length;z0++){
if(reg.test(els[z0].className)){
ary.push(els[z0]);
}
}
return ary;
}


}
function Projects(o){
var obj=document.getElementById(o.ID);
var panels=this.bycls(o.PanelClassName,obj);
this.w=obj.offsetWidth;
this.h=obj.offsetHeight;
this.panels=[];
for (var z0=0;z0<panels.length;z0++){
panels[z0].style.position='absolute';
panels[z0].style.visibility=z0>0?'hidden':'visible';
panels[z0].style.left='0px';
panels[z0].style.top='0px';
zxcOpacity(panels[z0],z0>0?0:100);
this.panels[z0]=new zxcAnimate('opacity',panels[z0],z0>0?0:100);
this.panels[z0].Complete=function(){
if (this.data[0]==0){
this.obj.style.visibility='hidden';
}
}
}
this.toggles=[];
obj=document.getElementById(o.NavID);
panels=this.bycls(o.NavClassName,obj);
for (var eobj,w,z1=0;z1<panels.length;z1++){
eobj=this.bycls(o.EventClassName,panels[z1])[0];
if (eobj){
this.addevt(eobj,'mouseup','Animate',z1);
}
eobj=this.bycls(o.CollapseClassName,panels[z1])[0];
if (eobj){
w=eobj.offsetWidth;
this.toggles[z1]=[new zxcAnimate('width',eobj,z1>0?0:w),w];
this.toggles[z1][0].Complete=function(){
if (this.data[0]==0){
this.obj.style.visibility='hidden';
}
}
eobj.style.overflow='hidden';
eobj.style.width=(z1>0?0:w)+'px';
}
}
this.max=panels[z1-1].offsetLeft+panels[z1-1].offsetWidth;
this.pw=obj.parentNode.offsetWidth;
this.ctr=new zxcAnimate('left',obj,this.max);
obj.style.left='0px'
this.ms=o.AnimateSpeed||1000;
this.nu=0;
this.Animate(0);
}

Projects.prototype={

Animate:function(nu){
var oop=this.panels[this.nu];
oop.animate(oop.data[0],0,this.ms,[0,100]);
if (this.toggles[this.nu]){
oop=this.toggles[this.nu];
oop[0].animate(oop[0].data[0],0,this.ms,[0,oop[1]]);
this.max-=oop[1];
}
this.ctr.animate(this.ctr.obj.offsetLeft,(this.pw-this.max)/2,this.ms);
oop=this.panels[nu];
oop.obj.style.left=(this.w-oop.obj.offsetWidth)/2+'px';
oop.obj.style.top=(this.h-oop.obj.offsetHeight)/2+'px';
oop.obj.style.visibility='visible';
oop.animate(oop.data[0],100,this.ms,[0,100]);
w=0;
if (this.toggles[nu]){
oop=this.toggles[nu];
oop[0].obj.style.visibility='visible';
oop[0].animate(oop[0].data[0],oop[1],this.ms,[0,oop[1]]);
this.max+=oop[1];
}
this.ctr.animate(this.ctr.data[0],(this.pw-this.max)/2,this.ms);
this.nu=nu;
},

addevt:function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](p,e);}, false);
else o.attachEvent('on'+t,function(e){ return oop[f](p,e); });
},

bycls:function (nme,el){
for (var reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName('*'),ary=[],z0=0; z0<els.length;z0++){
if(reg.test(els[z0].className)){
ary.push(els[z0]);
}
}
return ary;
}

}


new Projects({
ID:'panels',
PanelClassName:'panel',
NavID:'frames',
NavClassName:'float',
EventClassName:'projects',
CollapseClassName:'toggle',
AnimateSpeed:1000
});

new Gallery({
ID:'tst',
PanelClassName:'set',
ForwardID:'f1',
BackID:'b1',
PaginateID:'d1',
AnimateSpeed:1000,
SwapID:'nav',
SwapClassName:'swap'
});
/*]]>*/
</script>
</body>

</html>

Dex
12-18-2010, 08:55 PM
Wow you have done exactly what I was only hoping to achieve! Thank you very much for the time and effort that you have put into helping me :)

I wish you all the best.

- Dex