Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Location
    Wakefield, West Yorkshire
    Posts
    1,351
    Tokens
    0

    Latest Awards:

    Default [Tut] Drop-down menus

    Ok theres been a lot of people asking how to do this recently so I thought id make this. First, add the following code to the head of your page (between the <head> and </head> tags:
    HTML Code:
    <style type="text/css">
    .menutitle{
    cursor:pointer;
    margin-bottom: 5px;
    background-color:#ECECFF;
    color:#000000;
    width:140px;
    padding:2px;
    text-align:center;
    font-weight:bold;
    /*/*/border:1px solid #000000;/* */
    }
    
    .submenu{
    margin-bottom: 0.5em;
    }
    </style>
    
    <script type="text/javascript">
    
    /***********************************************
    * Switch Menu script- by Martial B of http://getElementById.com/
    * Modified by Dynamic Drive for format & NS4/IE4 compatibility
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
    var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only
    
    if (document.getElementById){ //DynamicDrive.com change
    document.write('<style type="text/css">\n')
    document.write('.submenu{display: none;}\n')
    document.write('</style>\n')
    }
    
    function SwitchMenu(obj){
        if(document.getElementById){
        var el = document.getElementById(obj);
        var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
            if(el.style.display != "block"){ //DynamicDrive.com change
                for (var i=0; i<ar.length; i++){
                    if (ar[i].className=="submenu") //DynamicDrive.com change
                    ar[i].style.display = "none";
                }
                el.style.display = "block";
            }else{
                el.style.display = "none";
            }
        }
    }
    
    function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function onloadfunction(){
    if (persistmenu=="yes"){
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=get_cookie(cookiename)
    if (cookievalue!="")
    document.getElementById(cookievalue).style.display="block"
    }
    }
    
    function savemenustate(){
    var inc=1, blockid=""
    while (document.getElementById("sub"+inc)){
    if (document.getElementById("sub"+inc).style.display=="block"){
    blockid="sub"+inc
    break
    }
    inc++
    }
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
    document.cookie=cookiename+"="+cookievalue
    }
    
    if (window.addEventListener)
    window.addEventListener("load", onloadfunction, false)
    else if (window.attachEvent)
    window.attachEvent("onload", onloadfunction)
    else if (document.getElementById)
    window.onload=onloadfunction
    
    if (persistmenu=="yes" && document.getElementById)
    window.onunload=savemenustate
    
    </script>
    Then use this code in your navigation:
    HTML Code:
    <!-- Keep all menus within masterdiv-->
    <div id="masterdiv">
    
        <div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
        <span class="submenu" id="sub1">
            - <a href="new.htm">What's New</a><br>
            - <a href="hot.htm">What's hot</a><br>
            - <a href="revised.htm">Revised Scripts</a><br>
            - <a href="morezone/">More Zone</a>
        </span>
    
    </div>
    and if u want it with an on click image:
    HTML Code:
    <!-- Keep all menus within masterdiv-->
    <div id="masterdiv">
    
        <img src="http://www.yoursite.com/image.png" onclick="SwitchMenu('sub1')"><br>
        <span class="submenu" id="sub1">
            - <a href="http://www.dynamicdrive.com/link.htm">Link to DD</a><br>
            - <a href="http://www.dynamicdrive.com/recommendit/">Recommend Us</a><br>
            - <a href="http://www.dynamicdrive.com/contact.htm">Email Us</a><br>
        </span>
    
    </div>
    as you add another drop down menu, change these lines:
    <div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
    <span class="submenu" id="sub1">

    so it looks like this:
    <div class="menutitle" onclick="SwitchMenu('sub2')">Site Menu</div>
    <span class="submenu" id="sub2">

    same goes 4 the image 1. Enjoy!

    Taken From;
    Switch Menu script- by Martial B of http://getElementById.com/
    Visit http://www.dynamicdrive.com/ for full source code


    Edited by L&#181;ke (Forum Moderator): Thread Moved From Website Designing. Please post in the correct section next time, Thanks .
    Last edited by Lµke; 16-02-2007 at 11:55 AM.

  2. #2
    Join Date
    Sep 2006
    Posts
    2,114
    Tokens
    0

    Latest Awards:

    Default

    Nice Tut.

    You should mention you got it from DynamicDrive.com
    Looking for a good desiner to design a social networking template.

    PM me.

  3. #3
    Join Date
    Sep 2004
    Location
    Liverpool
    Posts
    1,175
    Tokens
    52

    Latest Awards:

    Default

    Quote Originally Posted by sherbet View Post
    Ok theres been a lot of people asking how to do this recently so I thought id make this. First, add the following code to the head of your page (between the <head> and </head> tags:
    HTML Code:
    <style type="text/css">
    .menutitle{
    cursor:pointer;
    margin-bottom: 5px;
    background-color:#ECECFF;
    color:#000000;
    width:140px;
    padding:2px;
    text-align:center;
    font-weight:bold;
    /*/*/border:1px solid #000000;/* */
    }
    
    .submenu{
    margin-bottom: 0.5em;
    }
    </style>
    
    <script type="text/javascript">
    
    /***********************************************
    * Switch Menu script- by Martial B of http://getElementById.com/
    * Modified by Dynamic Drive for format & NS4/IE4 compatibility
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
    var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only
    
    if (document.getElementById){ //DynamicDrive.com change
    document.write('<style type="text/css">\n')
    document.write('.submenu{display: none;}\n')
    document.write('</style>\n')
    }
    
    function SwitchMenu(obj){
        if(document.getElementById){
        var el = document.getElementById(obj);
        var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
            if(el.style.display != "block"){ //DynamicDrive.com change
                for (var i=0; i<ar.length; i++){
                    if (ar[i].className=="submenu") //DynamicDrive.com change
                    ar[i].style.display = "none";
                }
                el.style.display = "block";
            }else{
                el.style.display = "none";
            }
        }
    }
    
    function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
    offset += search.length
    end = document.cookie.indexOf(";", offset);
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function onloadfunction(){
    if (persistmenu=="yes"){
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=get_cookie(cookiename)
    if (cookievalue!="")
    document.getElementById(cookievalue).style.display="block"
    }
    }
    
    function savemenustate(){
    var inc=1, blockid=""
    while (document.getElementById("sub"+inc)){
    if (document.getElementById("sub"+inc).style.display=="block"){
    blockid="sub"+inc
    break
    }
    inc++
    }
    var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
    var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
    document.cookie=cookiename+"="+cookievalue
    }
    
    if (window.addEventListener)
    window.addEventListener("load", onloadfunction, false)
    else if (window.attachEvent)
    window.attachEvent("onload", onloadfunction)
    else if (document.getElementById)
    window.onload=onloadfunction
    
    if (persistmenu=="yes" && document.getElementById)
    window.onunload=savemenustate
    
    </script>
    Then use this code in your navigation:
    HTML Code:
    <!-- Keep all menus within masterdiv-->
    <div id="masterdiv">
    
        <div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
        <span class="submenu" id="sub1">
            - <a href="new.htm">What's New</a><br>
            - <a href="hot.htm">What's hot</a><br>
            - <a href="revised.htm">Revised Scripts</a><br>
            - <a href="morezone/">More Zone</a>
        </span>
    
    </div>
    and if u want it with an on click image:
    HTML Code:
    <!-- Keep all menus within masterdiv-->
    <div id="masterdiv">
    
        <img src="http://www.yoursite.com/image.png" onclick="SwitchMenu('sub1')"><br>
        <span class="submenu" id="sub1">
            - <a href="http://www.dynamicdrive.com/link.htm">Link to DD</a><br>
            - <a href="http://www.dynamicdrive.com/recommendit/">Recommend Us</a><br>
            - <a href="http://www.dynamicdrive.com/contact.htm">Email Us</a><br>
        </span>
    
    </div>
    as you add another drop down menu, change these lines:
    <div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
    <span class="submenu" id="sub1">

    so it looks like this:
    <div class="menutitle" onclick="SwitchMenu('sub2')">Site Menu</div>
    <span class="submenu" id="sub2">

    same goes 4 the image 1. Enjoy!
    /***********************************************
    * Switch Menu script- by Martial B of http://getElementById.com/
    * Modified by Dynamic Drive for format & NS4/IE4 compatibility
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/



    Please do not try and claim others work as your own, or atleast make it look like your own! :rolleyes:
    Last edited by Lµke; 16-02-2007 at 10:55 AM.
    Please report all posts that break the forum rules by clicking the 'Report' post icon.

    Report Infraction Problems
    Report Reputation Problems

  4. #4
    Join Date
    Aug 2006
    Location
    Wakefield, West Yorkshire
    Posts
    1,351
    Tokens
    0

    Latest Awards:

    Default

    i wasnt. i should have referenced it sorry. not everyone knows where the script is.

  5. #5
    Join Date
    Sep 2004
    Location
    Liverpool
    Posts
    1,175
    Tokens
    52

    Latest Awards:

    Default

    Quote Originally Posted by sherbet View Post
    i wasnt. i should have referenced it sorry. not everyone knows where the script is.
    Haha no worries, i edited it for you now anyway .
    Please report all posts that break the forum rules by clicking the 'Report' post icon.

    Report Infraction Problems
    Report Reputation Problems

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •