Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Menu rozwijane
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
kamil_lk
Witam.

Mam gotowy skrypt, dzięki któremu można otrzymać rozwijane menu.

Wszystko śmiga jak należy, tzn prawie wszystko.

Aby menu pozostawało rozwinięte podczas przechodzenia z jednej strony na drugą to odnośnik MUSI znajdować się w menu, np.

index.php?section=User <- taki odnośnik mamy w menu i jest rozwinięte menu dotyczące Użytkowników, ale gdy przejdziemy pod adres
index.php?section=User&method=add <- to menu się zamyka ponieważ nie ma takiego odnośnika w nim zawartego

Czy mógłby ktoś przerobić skrypt, aby po rozszerzeniu adresu o kolejne pozycje menu pozostawało otwarte?

Pozdrawiam.

  1. /**
  2. * @author Slawomir Koklowski {@link <a href="http://www.kurshtml.boo.pl}" target="_blank">http://www.kurshtml.boo.pl}</a>
  3. * @copyright NIE usuwaj tego komentarza! (Do NOT remove this comment!)
  4. */
  5.  
  6. function Menu(id, style, otworz, wysun, czasRozwin, czasZwin, czasOtworz, czasZamknij, nieInicjalizuj)
  7. {
  8. if (typeof czasRozwin == 'undefined' || czasRozwin < 0) czasRozwin = 25;
  9. if (typeof czasZwin == 'undefined' || czasZwin < 0) czasZwin = 25;
  10. if (typeof czasOtworz == 'undefined' || czasOtworz < 0) czasOtworz = 250;
  11. if (typeof czasZamknij == 'undefined' || czasZamknij < 0) czasZamknij = 500;
  12.  
  13. var url = unescape(window.location.href.replace(/#.*/, ''));
  14. var base = window.location.protocol + '//' + window.location.host + window.location.pathname.replace(/[^\/\\]+$/, '');
  15.  
  16. if (style)
  17. {
  18. if (style.indexOf(':') < 0)
  19. {
  20. document.getElementById(id).className += ' ' + style;
  21. }
  22. else
  23. {
  24. style = style.replace(/(^\s+|(\s|;)+$)/g, '').split(/\s*;\s*/);
  25. for (var i = 0; i < style.length; i++)
  26. {
  27. style[i] = style[i].split(/\s*:\s*/);
  28. for (var j = 0, c, property = ''; j < style[i][0].length; j++)
  29. {
  30. c = style[i][0].charAt(j);
  31. property += c == '-' ? style[i][0].charAt(++j).toUpperCase() : c.toLowerCase();
  32. }
  33. eval('document.getElementById("' + id + '").style.' + property + ' = "' + style[i][1].replace(/"/g, '\\"') + '"');
  34. }
  35. }
  36. }
  37. for (var i = 0; i < document.getElementById(id).getElementsByTagName('dt').length; i++)
  38. {
  39. var dd = new Array();
  40. var el = document.getElementById(id).getElementsByTagName('dt')[i].nextSibling;
  41. var nodeName;
  42. while (el && (nodeName = el.nodeName.toLowerCase()) != 'dt')
  43. {
  44. if (nodeName == 'dd')
  45. {
  46. el._dt = document.getElementById(id).getElementsByTagName('dt')[i];
  47. if (otworz)
  48. {
  49. el.onmouseover = function()
  50. {
  51. clearTimeout(this._dt._timoutID);
  52. this._dt._displayed = false;
  53. this._dt.onclick();
  54. }
  55. el.onmouseout = function()
  56. {
  57. clearTimeout(this._dt._timoutID);
  58. var dt = this._dt;
  59. this._dt._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
  60. };
  61. }
  62. dd[dd.length] = el;
  63. }
  64. el = el.nextSibling;
  65. }
  66. document.getElementById(id).getElementsByTagName('dt')[i]._dd = dd;
  67. document.getElementById(id).getElementsByTagName('dt')[i]._timoutID = null;
  68. document.getElementById(id).getElementsByTagName('dt')[i]._displayed = false;
  69. document.getElementById(id).getElementsByTagName('dt')[i].onclick = function()
  70. {
  71. clearTimeout(this._timoutID);
  72. if (!this._displayed)
  73. {
  74. var el = this.parentNode.getElementsByTagName('dt')[0];
  75. while (el)
  76. {
  77. if (el.nodeName.toLowerCase() == 'dt' && el != this)
  78. {
  79. el._displayed = false;
  80. if (czasZwin) display(el, 0);
  81. else display(el);
  82. }
  83. el = el.nextSibling;
  84. }
  85. }
  86. this._displayed = !this._displayed;
  87. if (this._displayed && czasRozwin || !this._displayed && czasZwin) display(this, 0);
  88. else display(this);
  89. };
  90. if (otworz)
  91. {
  92. document.getElementById(id).getElementsByTagName('dt')[i].onmouseover = function()
  93. {
  94. clearTimeout(this._timoutID);
  95. var dt = this;
  96. this._timoutID = setTimeout(function () { dt._displayed = false; dt.onclick(); }, czasOtworz);
  97. };
  98. document.getElementById(id).getElementsByTagName('dt')[i].onmouseout = function()
  99. {
  100. clearTimeout(this._timoutID);
  101. var dt = this;
  102. this._timoutID = setTimeout(function () { dt._displayed = true; dt.onclick(); }, czasZamknij);
  103. };
  104. }
  105. }
  106. start(document.getElementById(id).getElementsByTagName('dt')[0]);
  107. function start(dt)
  108. {
  109. var hide = true;
  110. var el = dt;
  111. while (el)
  112. {
  113. var nodeName = el.nodeName.toLowerCase();
  114. if (nodeName == 'dt')
  115. {
  116. dt = el;
  117. hide = true;
  118. }
  119. if (nodeName == 'dt' || nodeName == 'dd')
  120. {
  121. if (!nieInicjalizuj && el.getElementsByTagName('a').length)
  122. {
  123. var active = el.getElementsByTagName('a')[0].href && unescape(el.getElementsByTagName('a')[0].href.replace(/#.*/, '')) == url;
  124. if (!active)
  125. {
  126. var rel = el.getElementsByTagName('a')[0].getAttribute('rel');
  127. if (rel)
  128. {
  129. var matches = (' ' + rel + ' ').match(/\s+Collection\(([^)]+)\)\s+/i);
  130. if (matches)
  131. {
  132. matches = matches[1].split(',');
  133. for (var k = 0, pos = -1; k < matches.length; k++)
  134. {
  135. if (matches[k].charAt(0) == '[' && (pos = matches[k].lastIndexOf(']')) > 0)
  136. {
  137. if (new RegExp(unescape(matches[k].substring(1, pos)), matches[k].substring(pos + 1)).test(url))
  138. {
  139. active = true;
  140. break;
  141. }
  142. }
  143. else
  144. {
  145. if (/^[\/\\]/.test(matches[k])) matches[k] = window.location.protocol + '//' + window.location.host + matches[k];
  146. else if (!/^[a-z0-9]+:/i.test(matches[k])) matches[k] = base + matches[k];
  147. if (unescape(matches[k].replace(/[\/\\]\.([\/\\])/g, '$1').replace(/[^\/\\]+[\/\\]\.\.[\/\\]/g, '').replace(/#.*/, '')) == url)
  148. {
  149. active = true;
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. if (active)
  158. {
  159. el.className = (el.className ? el.className + ' ' : '') + 'active';
  160. dt._displayed = true;
  161. display(dt);
  162. hide = false;
  163. var el_parentNode = el.parentNode;
  164. while (el_parentNode != document.getElementById(id))
  165. {
  166. if (el_parentNode.nodeName.toLowerCase() == 'dd')
  167. {
  168. var el_sibling = el_parentNode.previousSibling;
  169. while (el_sibling)
  170. {
  171. if (el_sibling.nodeName.toLowerCase() == 'dt')
  172. {
  173. el_sibling._displayed = true;
  174. display(el_sibling)
  175. break;
  176. }
  177. el_sibling = el_sibling.previousSibling;
  178. }
  179. }
  180. el_parentNode = el_parentNode.parentNode;
  181. }
  182. }
  183. }
  184. }
  185. if (nodeName == 'dd')
  186. {
  187. if (hide) el.style.display = 'none';
  188. start(el.getElementsByTagName('dt')[0]);
  189. }
  190. el = el.nextSibling;
  191. }
  192. }
  193.  
  194. function display(dt, i)
  195. {
  196. if (typeof i == 'undefined')
  197. {
  198. for (var i = 0; i < dt._dd.length; i++)
  199. {
  200. dt._dd[i].style.display = dt._displayed ? 'block' : 'none';
  201. if (!dt._displayed)
  202. {
  203. for (var j = 0; j < dt._dd[i].getElementsByTagName('dt').length; j++)
  204. {
  205. dt._dd[i].getElementsByTagName('dt')[j]._displayed = false;
  206. display(dt._dd[i].getElementsByTagName('dt')[j]);
  207. }
  208. }
  209. }
  210. }
  211. else if (i < dt._dd.length)
  212. {
  213. var dir = wysun ? !dt._displayed : dt._displayed;
  214. var n = dir ? i : dt._dd.length - 1 - i;
  215. dt._dd[n].style.display = dt._displayed ? 'block' : 'none';
  216. if (!dt._displayed)
  217. {
  218. for (var j = 0; j < dt._dd[n].getElementsByTagName('dt').length; j++)
  219. {
  220. dt._dd[n].getElementsByTagName('dt')[j]._displayed = false;
  221. display(dt._dd[n].getElementsByTagName('dt')[j]);
  222. }
  223. }
  224. dt._timoutID = setTimeout(function() { display(dt, i + 1); }, dt._displayed ? czasRozwin : czasZwin);
  225. }
  226. }
  227. }
malum
Witam!
Czy rozwiązałeś może swój problem, bo walczę dokładnie z tym samym sad.gif
kamil_lk
Nie rozwiązałem. Ale napisałem coś podobnego, krótszego.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Untitled Document</title>
  5. <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
  6. <style type="text/css">
  7. ul ul{ display: none; }
  8. </head>
  9.  
  10.  
  11. <ul>
  12. <li><span>Menu 1</span>
  13. <ul>
  14. <li><span>Menu 1.1</span></li>
  15. <li><span>Menu 1.2</span>
  16. <ul>
  17. <li><span>Menu 1.2.1</span>
  18. <ul>
  19. <li><span>Menu 1.2.1.1</span></li>
  20. </ul>
  21. </li>
  22. <li><span>Menu 1.2.2</span></li>
  23. </ul>
  24. </li>
  25. <li><span><span>Menu 1.3</span></li>
  26. </ul>
  27. </li>
  28. <li><span>Menu 2</span>
  29. <ul>
  30. <li><span>Menu 2.1</span></li>
  31. <li><span>Menu 2.2</span>
  32. <ul>
  33. <li><span>Menu 2.2.1</span></li>
  34. <li><span>Menu 2.2.2</span></li>
  35. <li><span>Menu 2.2.3</span></li>
  36. </ul>
  37. </li>
  38. <li><span>Menu 2.3</span></li>
  39. </ul>
  40. </li>
  41. <li><span>Menu 3</span></li>
  42. </ul>
  43. <script type="text/javascript">
  44. $(window).ready(function(){
  45. $("li span").bind('click', function() {
  46. if($(this).next("ul").css("display") == 'block')
  47. $(this).next("ul").hide("300");
  48. else
  49. $(this).next("ul").show("300");
  50. });
  51. });
  52. </body>
  53. </html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.