[Résolu] Ajouter une option à ce menu

WRInaute discret
Bonjour à tous,
Voici un menu vertical coulissant que j'utiliserais bien pour la grande arborescence de mon site...
Par contre je voudrais lui ajouter une option : lorsqu'un menu est ouvert et que je veux en ouvrir un autre, le premier ce ferme (afin qu'il ne s'étende pas trop sur la page)

Voici le code entier :

Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.sdmenu {
    width: 150px;
    font-family: Sans-Serif;
    font-size: 12px;
    padding-bottom: 10px;
    background: #eee url(bottom.jpg) no-repeat  right bottom;
    color: #FFF;
}

.sdmenu .title, .sdmenu .titlehidden{
    display: block;
    padding: 5px 0;
    font-weight: bold;
    color: white;
    background: #FFF url(toptitle.jpg) repeat-x;
}

.sdmenu .title {
    border-bottom: 1px solid yellow;
}

.sdmenu .arrow {
		margin-left: 10px;
    margin-right: 7px;
}

.sdmenu .titlehidden {
    border-bottom: none;
}

.sdmenu #top {
    background: url(toptitle.jpg) no-repeat;
}

.sdmenu .submenu {
    overflow: hidden;
}

.sdmenu .submenu a {
    padding: 5px 0;
    text-indent: 10px;
    background: black;
    text: black;
    display: block;
    border-bottom: 1px solid yellow;
    color: gray;
    text-decoration: none;

}

.sdmenu .submenu a:hover {
    background : #600040 url(linkarrow.gif) no-repeat right center;
    color: #FFF;
}
</style>
<SCRIPT LANGUAGE=JavaScript>
var remember = true; //Remember menu states, and restore them on next visit.
var contractall_default= true; //Should all submenus be contracted by default? (true or false)

var menu, titles, submenus, arrows, bypixels;
var heights = new Array();

var n = navigator.userAgent;
if(/Opera/.test(n)) bypixels = 2;
else if(/Firefox/.test(n)) bypixels = 3;
else if(/MSIE/.test(n)) bypixels = 2;

/////DD added expandall() and contractall() functions/////

function slash_expandall(){
if (typeof menu!="undefined"){
	for(i=0; i<Math.max(titles.length, submenus.length); i++){
		titles[i].className="title";
		arrows[i].src = "slashfiles/expanded.gif";
		submenus[i].style.display="";
		submenus[i].style.height = heights[i]+"px";
	}
}
}

function slash_contractall(){
if (typeof menu!="undefined"){
	for(i=0; i<Math.max(titles.length, submenus.length); i++){
		titles[i].className="titlehidden";
		arrows[i].src = "slashfiles/collapsed.gif";
		submenus[i].style.display="none";
		submenus[i].style.height = 0;
	}
}
}

/////End DD added functions///////////////////////////////


function init(){
    menu = getElementsByClassName("sdmenu", "div", document)[0];
    titles = getElementsByClassName("title", "span", menu);
    submenus = getElementsByClassName("submenu", "div", menu);
    arrows = getElementsByClassName("arrow", "img", menu);
    for(i=0; i<Math.max(titles.length, submenus.length); i++) {
        titles[i].onclick = gomenu;
        arrows[i].onclick = gomenu;
        heights[i] = submenus[i].offsetHeight;
        submenus[i].style.height = submenus[i].offsetHeight+"px";
    }
    if(remember)
				restore()
		else if (contractall_default) //DD added code
				slash_contractall() //DD added code
}

function restore() {
    if(getcookie("menu") != null) {
        var hidden = getcookie("menu").split(",");
        for(var i in hidden) {
            titles[hidden[i]].className = "titlehidden";
            submenus[hidden[i]].style.height = "0px";
            submenus[hidden[i]].style.display = "none";
            arrows[hidden[i]].src = "slashfiles/collapsed.gif";
        }
    }
}

function gomenu(e) {
    if (!e)
        var e = window.event;
    var ce = (e.target) ? e.target : e.srcElement;
    var sm;
    for(var i in titles) {
        if(titles[i] == ce || arrows[i] == ce)
            sm = i;
    }
    if(parseInt(submenus[sm].style.height) > parseInt(heights[sm])-2) {
        hidemenu(sm);
    } else if(parseInt(submenus[sm].style.height) < 2) {
        titles[sm].className = "title";
        showmenu(sm);
    }
}

function hidemenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)-nr)+"px";
    var to = setTimeout("hidemenu("+sm+")", 30);
    if(parseInt(submenus[sm].style.height) <= nr) {
        clearTimeout(to);
        submenus[sm].style.display = "none";
        submenus[sm].style.height = "0px";
        arrows[sm].src = "slashfiles/collapsed.gif";
        titles[sm].className = "titlehidden";
    }
}

function showmenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.display = "";
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)+nr)+"px";
    var to = setTimeout("showmenu("+sm+")", 30);
    if(parseInt(submenus[sm].style.height) > (parseInt(heights[sm])-nr)) {
        clearTimeout(to);
        submenus[sm].style.height = heights[sm]+"px";
        arrows[sm].src = "slashfiles/expanded.gif";
    }
        
        
}

function store() {
    var hidden = new Array();
    for(var i in titles) {
        if(titles[i].className == "titlehidden")
            hidden.push(i);
    }
    putcookie("menu", hidden.join(","), 30);
}

function getElementsByClassName(strClassName, strTagName, oElm){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function putcookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate);
}

function getcookie(c_name) {
    if(document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if(c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";",c_start);
            if(c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

window.onload = init;
if(remember) window.onunload = store;

</SCRIPT>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<div class="sdmenu">
     <span class="title" id="top"><img src="slashfiles/expanded.gif" class="arrow" alt="*" />A</span>
      <div class="submenu">
        <b>
	<a href="1.html"><b>1</b></a>
        <a href="2.html">2</a>
        <a href="3.html">3</a>
      </b></div><b>
      <span class="title" id="top"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />B</span>
      <div class="submenu">
        <a href="11.html"><b>1</b></a>
        <a href="12.html">2</a>
        <a href="13.html/">3</a>
        <a href="14.html/">4</a>
        <a href="15.html">5</a>
        <a href="16.html">6</a>
        <a href="17.html">7</a>
        <a href="18.html">8</a>
        <a href="19.html">9</a>
      </div>
      <span class="title"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />C</span>
      <div class="submenu">
        <a href="21.html"><b>1</b></a>
        <a href="22.html">2</a>
        <a href="23.html/">3</a>
        <a href="24.html/">4</a>
        <a href="25.html">5</a>
        <a href="26.html">6</a>
        <a href="27.html">7</a>
        <a href="28.html">8</a>
        <a href="29.html">9</a>
      </div>
      <span class="title"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />D</span>
      </b><div class="submenu"><b>
        <a href="31.html">1</a>
        <a href="32.html">2</a>
        <a href="33.html">3</a>
        <a href="34.html">4</a></b>
      </div>
    </div>
</body>
</html>

Si quelqu'un pourrais toucher au javascript pour rendre cette action possible ce serait très sympa!
Merci d'avance et JOYEUX NOEL à tous!
 
WRInaute discret
Je n'est toujours pas trouvé de solution...
Ne faudrait-il pas ajouter une variable sur le nombre de "branche" ouverte (une en l'occurrence) ?
Le probleme de ce menu bloque le développement de la nouvelle version de mon site, je vous remercierais de m'aider
 
WRInaute discret
j'ai maté vite fait voilà ce que je te propose :

1 - ajouter une fonction smooth_contractall qui appellera la fonction hidemenu existente pour tous les menus ouverts
2 - à chaque ouverture de menu, appeler cette smooth_contractall

(note, ca marchait avec la fonction existante slash_contractall mais sans glisser doux)

Code:
<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<style type="text/css"> 
.sdmenu { 
    width: 150px; 
    font-family: Sans-Serif; 
    font-size: 12px; 
    padding-bottom: 10px; 
    background: #eee url(bottom.jpg) no-repeat  right bottom; 
    color: #FFF; 
} 

.sdmenu .title, .sdmenu .titlehidden{ 
    display: block; 
    padding: 5px 0; 
    font-weight: bold; 
    color: white; 
    background: #FFF url(toptitle.jpg) repeat-x; 
} 

.sdmenu .title { 
    border-bottom: 1px solid yellow; 
} 

.sdmenu .arrow { 
      margin-left: 10px; 
    margin-right: 7px; 
} 

.sdmenu .titlehidden { 
    border-bottom: none; 
} 

.sdmenu #top { 
    background: url(toptitle.jpg) no-repeat; 
} 

.sdmenu .submenu { 
    overflow: hidden; 
} 

.sdmenu .submenu a { 
    padding: 5px 0; 
    text-indent: 10px; 
    background: black; 
    text: black; 
    display: block; 
    border-bottom: 1px solid yellow; 
    color: gray; 
    text-decoration: none; 

} 

.sdmenu .submenu a:hover { 
    background : #600040 url(linkarrow.gif) no-repeat right center; 
    color: #FFF; 
} 
</style> 
<SCRIPT LANGUAGE=JavaScript> 
var remember = true; //Remember menu states, and restore them on next visit. 
var contractall_default= true; //Should all submenus be contracted by default? (true or false) 

var menu, titles, submenus, arrows, bypixels; 
var heights = new Array(); 

var n = navigator.userAgent; 
if(/Opera/.test(n)) bypixels = 2; 
else if(/Firefox/.test(n)) bypixels = 3; 
else if(/MSIE/.test(n)) bypixels = 2; 

/////DD added expandall() and contractall() functions///// 

function slash_expandall(){ 
if (typeof menu!="undefined"){ 
   for(i=0; i<Math.max(titles.length, submenus.length); i++){ 
      titles[i].className="title"; 
      arrows[i].src = "slashfiles/expanded.gif"; 
      submenus[i].style.display=""; 
      submenus[i].style.height = heights[i]+"px"; 
   } 
} 
} 

function slash_contractall(){ 
if (typeof menu!="undefined"){ 
   for(i=0; i<Math.max(titles.length, submenus.length); i++){ 
	   
	   titles[i].className="titlehidden"; 
      arrows[i].src = "slashfiles/collapsed.gif"; 
      submenus[i].style.display="none"; 
      submenus[i].style.height = 0; 
   } 
} 
} 

function smooth_contractall(){ 
if (typeof menu!="undefined"){ 
   for(i=0; i<Math.max(titles.length, submenus.length); i++){
	   if(submenus[i].style.display != "none" && submenus[i].style.height != "0px")
	   {
			hidemenu(i);
  	   }
   } 
} 
} 


/////End DD added functions/////////////////////////////// 


function init(){ 
    menu = getElementsByClassName("sdmenu", "div", document)[0]; 
    titles = getElementsByClassName("title", "span", menu); 
    submenus = getElementsByClassName("submenu", "div", menu); 
    arrows = getElementsByClassName("arrow", "img", menu); 
    for(i=0; i<Math.max(titles.length, submenus.length); i++) { 
        titles[i].onclick = gomenu; 
        arrows[i].onclick = gomenu; 
        heights[i] = submenus[i].offsetHeight; 
        submenus[i].style.height = submenus[i].offsetHeight+"px"; 
    } 
    if(remember) 
            restore() 
      else if (contractall_default) //DD added code 
            slash_contractall() //DD added code 
} 

function restore() { 
    if(getcookie("menu") != null) { 
        var hidden = getcookie("menu").split(","); 
        for(var i in hidden) { 
            titles[hidden[i]].className = "titlehidden"; 
            submenus[hidden[i]].style.height = "0px"; 
            submenus[hidden[i]].style.display = "none"; 
            arrows[hidden[i]].src = "slashfiles/collapsed.gif"; 
        } 
    } 
} 

function gomenu(e) { 
	smooth_contractall();
    if (!e) 
        var e = window.event; 
    var ce = (e.target) ? e.target : e.srcElement; 
    var sm; 
    for(var i in titles) { 
        if(titles[i] == ce || arrows[i] == ce) 
            sm = i; 
    } 
    if(parseInt(submenus[sm].style.height) > parseInt(heights[sm])-2) { 
        hidemenu(sm); 
    } else if(parseInt(submenus[sm].style.height) < 2) { 
        titles[sm].className = "title"; 
        showmenu(sm); 
    } 
} 

function hidemenu(sm) { 
	
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels; 
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)-nr)+"px"; 
    var to = setTimeout("hidemenu("+sm+")", 30); 
    
    if(parseInt(submenus[sm].style.height) <= nr) { 
        clearTimeout(to); 
        submenus[sm].style.display = "none"; 
        submenus[sm].style.height = "0px"; 
        arrows[sm].src = "slashfiles/collapsed.gif"; 
        titles[sm].className = "titlehidden"; 
    } 
} 

function showmenu(sm) { 
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels; 
    submenus[sm].style.display = ""; 
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)+nr)+"px"; 
    var to = setTimeout("showmenu("+sm+")", 30); 
    if(parseInt(submenus[sm].style.height) > (parseInt(heights[sm])-nr)) { 
        clearTimeout(to); 
        submenus[sm].style.height = heights[sm]+"px"; 
        arrows[sm].src = "slashfiles/expanded.gif"; 
    } 
        
        
} 

function store() { 
    var hidden = new Array(); 
    for(var i in titles) { 
        if(titles[i].className == "titlehidden") 
            hidden.push(i); 
    } 
    putcookie("menu", hidden.join(","), 30); 
} 

function getElementsByClassName(strClassName, strTagName, oElm){ 
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName); 
    var arrReturnElements = new Array(); 
    strClassName = strClassName.replace(/\-/g, "\\-"); 
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)"); 
    var oElement; 
    for(var i=0; i<arrElements.length; i++){ 
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){ 
            arrReturnElements.push(oElement); 
        }    
    } 
    return (arrReturnElements) 
} 

function putcookie(c_name,value,expiredays) { 
    var exdate=new Date(); 
    exdate.setDate(exdate.getDate()+expiredays); 
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate); 
} 

function getcookie(c_name) { 
    if(document.cookie.length > 0) { 
        var c_start = document.cookie.indexOf(c_name + "="); 
        if(c_start != -1) { 
            c_start = c_start + c_name.length + 1; 
            var c_end = document.cookie.indexOf(";",c_start); 
            if(c_end == -1) 
                c_end = document.cookie.length; 
            return unescape(document.cookie.substring(c_start, c_end)); 
        } 
    } 
    return null; 
} 

window.onload = init; 
if(remember) window.onunload = store; 

</SCRIPT> 
</head> 

<body bgcolor="#FFFFFF" text="#000000"> 
<div class="sdmenu"> 
     <span class="title" id="top"><img src="slashfiles/expanded.gif" class="arrow" alt="*" />A</span> 
      <div class="submenu"> 
        <b> 
   <a href="1.html"><b>1</b></a> 
        <a href="2.html">2</a> 
        <a href="3.html">3</a> 
      </b></div><b> 
      <span class="title" id="top"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />B</span> 
      <div class="submenu"> 
        <a href="11.html"><b>1</b></a> 
        <a href="12.html">2</a> 
        <a href="13.html/">3</a> 
        <a href="14.html/">4</a> 
        <a href="15.html">5</a> 
        <a href="16.html">6</a> 
        <a href="17.html">7</a> 
        <a href="18.html">8</a> 
        <a href="19.html">9</a> 
      </div> 
      <span class="title"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />C</span> 
      <div class="submenu"> 
        <a href="21.html"><b>1</b></a> 
        <a href="22.html">2</a> 
        <a href="23.html/">3</a> 
        <a href="24.html/">4</a> 
        <a href="25.html">5</a> 
        <a href="26.html">6</a> 
        <a href="27.html">7</a> 
        <a href="28.html">8</a> 
        <a href="29.html">9</a> 
      </div> 
      <span class="title"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />D</span> 
      </b><div class="submenu"><b> 
        <a href="31.html">1</a> 
        <a href="32.html">2</a> 
        <a href="33.html">3</a> 
        <a href="34.html">4</a></b> 
      </div> 
    </div> 
</body> 
</html>

Je gagne qque chose ? :D
 
Nouveau WRInaute
Bonjour,
Est il possible de faire en sorte que tous les menus soient fermés par défaut lors de la première ouverture de la page?
Si oui, comment ?
Merci.
 
Discussions similaires
Haut