Years ago I had to implement a drop-down menu. Nothing fancy; no open-on-hover magic, but a simple line of buttons with drop-down boxes that would open on clicking the button.
There were just a few annoying details: clicking a top-row button should obviously open a drop-down box, but also change state the button’s state to “depressed” and close any other open drop-down box (and change the state of corresponding buttons).
Here is my five-year-old HTML code …
<div style="float: left; position: relative; z-index: 100;" id="IDAHYJQB"> <p class="btn170"> <a href="/climbing/myClimbs/myClimbs.asp">First page</a> </p> </div> <div style="float: left; position: relative; z-index: 100;" id="IDAKYJQB"> <script>menuRegister('IDAKYJQB')</script> <p class="btn170" id="IDAKYJQB_main"> <a href="javascript:menuClick('IDAKYJQB')">Add ...</a> </p> <div style="position: absolute; display: none;" id="IDAKYJQB_sub"> <p class="btn170"> <a onclick="menuSelect('IDAKYJQB')" href="/climbing/myClimbs/myClimbs_add.asp">New entry </a> </p> <p class="btn170"> <a onclick="menuSelect('IDAKYJQB')" href="/climbing/myClimbs/myClimbs_editWall.asp?a=add">Edit</a> </p> … more … </div> </div>
… the corresponding CSS …
.btn170, .btn170_down { background-repeat: no-repeat; width: 170px; height: 20px; line-height: 18px; overflow: hidden; padding: 0 0; margin: 0 0; text-align: center; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: 700; font-size: 11px; } .btn170 { background-image: url('images/button_170.gif'); } .btn170_down { background-image: url('images/button_down_170.gif'); }
… and JavaScript code …
var topMenuItems = [] ; function addClass(id,sfx) { var se = getElement(id) ; if (se.className.indexOf(sfx) < 0) se.className = se.className + sfx ; } function removeClass(id,sfx) { var se = getElement(id) ; var i = se.className.indexOf(sfx) ; if (i > 0) se.className = se.className.substr(0,i) ; } function menuShow(id) { var se = getElement(id) ; se.menuActive = true ; showElement(id + "_sub") ; addClass(id+"_main","_down"); } function menuHide(id) { var se = getElement(id) ; se.menuActive = false ; hideElement(id + "_sub") ; removeClass(id+"_main","_down"); } function menuGo(id,l) { menuHide(id); location.href = l; } function menuSelect(id) { menuHide(id) ; } function menuClick(id) { var i,se ; se = getElement(id) ; if (se.menuActive) { menuHide(id) ; return ; } for (i = 0 ; i < topMenuItems.length ; i++) { if (topMenuItems[i] != id) menuHide(topMenuItems[i]); } menuShow(id) ; } function menuRegister(id) { topMenuItems[topMenuItems.length] = id ; }
I will not try to explain what this code does, as it’s way too painful. As I’ll walk through the refactoring process, I’ll show you the changes I’ve made and the stupidities in the original code.
No comments:
Post a Comment