?
Solved

JQuery Slide Menu Slide Toggle on click

Posted on 2010-09-08
10
Medium Priority
?
2,337 Views
Last Modified: 2012-05-10
Hi,

I have this jquery slide menu code that I am having problems getting the menu to close on the second click.  So that if a menu is open and you click it again it will close.  AS it is right now it  will not close it just shows the menu again.  

I would also like for an image to open and close based on if the menu is open or closed.  I am new to jquery and I am having problems figuring this out.  

Thanks for the help.  

Kevin


Here is the code:  
    
    <TITLE>Slide Demo - Fixed Drawer</TITLE>
    <style type="text/css">
    BODY { margin: 10px; padding: 0; font-family: "Helvetica Neue', Helvetica, Arial, Sans-Serif";}
H2 { font-size: 20px;}
    UL.drawers {
        width: 317px;
       padding-bottom: 10px;
    }
    
    UL {
        list-style: none;
        margin: 0;
        padding: 5px;
    }
    
    LI.drawerssub A {
        text-decoration: none;
        color: #000000;
        list-style: none;
        
    }
    
       LI.drawerssub A:HOVER {
        text-decoration: none;
        color: #E94C24;
        
    }
    
    
    H2.drawer-handle {
        margin: 0;
        padding: 10px;
        background-color: #BAB1AD;
        height: 28px;
        margin-bottom: 3px;
        list-style: none;
    }
    
    H2.drawer-handle.open {
        background-color: #BAB1AD;
        list-style: none;
    }
        
    -->
    </STYLE>

    <SCRIPT src="./Slide Demo - Fixed Drawer_files/jquery-1.2.1.js" type="text/javascript"></SCRIPT>

    <SCRIPT type="text/javascript">
    <!--    
    $(document).ready(function () {
      $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
      $('H2.drawer-handle').click(function () {
        // hide all the drawer contents
        $('LI.drawer UL:visible').slideUp().prev().removeClass('open');

        // show the associated drawer content to 'this' (this is the current H2 element)
        // since the drawer content is the next element after the clicked H2, we find
        // it and show it using this:
        $(this).addClass('open').next().slideDown();
      });
    });
    //-->    </SCRIPT>
</HEAD><BODY id="page">
    
    <DIV class="drawers-wrapper">
	<DIV class="boxcap captop"></DIV>
    <UL class="drawers" id="nav">
        <LI class="drawer" style="position: static; ">
            <H2 class="drawer-handle open"><IMG src="RightArrow.gif" />     WORSHIP</H2>
            <UL class="alldownloads" style="display: block; ">
         <li class="drawerssub"><a href="#">Sunday Worship</a> </li>
        <li class="drawerssub"><a href="#">Childcare</a> </li>
        <li class="drawerssub"><a href="#">Current Sermon Series</a> </li>
        <li class="drawerssub"><a href="#">Audio & Study Guides</a> </li>
        <li class="drawerssub"><a href="#">Music & Choirs</a> </li>
        <li class="drawerssub"><a href="#">Worship at Union</a> </li>
            </UL>
        </LI>
        <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     CONNECT TO COMMUNITY</H2>
            <UL style="display: none; ">
 
        <li class="drawerssub"><a href="#">Classes</a> </li>
        <li class="drawerssub"><a href="#">Events</a> </li>
        <li class="drawerssub"><a href="#">Baptism</a> </li>
        <li class="drawerssub"><a href="#">Membership</a> </li>
        <li class="drawerssub"><a href="#">Caring Ministries and Resources</a> </li>
            </UL>
        </LI>
        <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     SERVE</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Serve around UPC</a> </li>
        <li class="drawerssub"><a href="#">Serve around Seattle</a> </li>
        <li class="drawerssub"><a href="#">Serve around the World</a> </li>
            </ul>
            </LI>
         <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     SMALL GROUPS</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Join a Group</a> </li>
        <li class="drawerssub"><a href="#">For Group Facilitators</a> </li>
        <li class="drawerssub"><a href="#">Sermon Study Guides & Audio</a> </li>
        <li class="drawerssub"><a href="#">Ideas for Serving Together</a> </li>
            </ul>
            </li>
            <LI class="drawer">
            <H2 class="drawer-handle"><IMG src="RightArrow.gif" />     MINISTRIES</H2>
            <UL style="display: none; ">
        <li class="drawerssub"><a href="#">Children & Families</a> </li>
        <li class="drawerssub"><a href="#">Youth Mission & Ministry</a> </li>
        <li class="drawerssub"><a href="#">University</a> </li>
        <li class="drawerssub"><a href="#">20/30s</a> </li>
        <li class="drawerssub"><a href="#">Adults</a> </li>
        <li class="drawerssub"><a href="#">ESL & Internationals</a> </li>
        <li class="drawerssub"><a href="#">Side-by-Side</a> </li>
        <li class="drawerssub"><a href="#">Hospitality</a> </li>

            </UL>
        </LI>
    </UL>
	<DIV class="boxcap"></DIV>
	</DIV>

</BODY></HTML>

Open in new window

0
Comment
Question by:upchelpdesk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:jayantc
ID: 33633790
Here is an example:

http://jqueryui.com/demos/accordion/#collapsible

- JayantC
0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33634341
Hi, you can use this:

      $(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
            $('H2.drawer-handle').click(function () {
                  $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
                  $(this).not(".MarkClosed").addClass('open').next().slideDown();
                  $(".MarkClosed").removeClass("MarkClosed");
            });
      });

Also, if you want to show some other Image, where is the image, and when to show?

Regards.
0
 
LVL 1

Author Comment

by:upchelpdesk
ID: 33638506
senthurpandian,

Thanks for the reply, the image I want to show is found in this line:  

<H2 class="drawer-handle open"><IMG src="RightArrow.gif" />  

If Open I would change it to like <H2 class="drawer-handle open"><IMG src="DownArrow.gif" />

Make sense?  
0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 

Expert Comment

by:tushar-mahajan
ID: 33644078
Hi just add few lines in your code
 
$(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
            $('H2.drawer-handle').click(function () {
                // your code
                $('H2.drawer-handle').find('IMG').attr("src","RightArrow.gif");
            $(this).find('IMG').attr("src","DownArrow.gif");
            });
});
0
 
LVL 1

Author Comment

by:upchelpdesk
ID: 33650286
That code works but does not change the arrow back to the RightArrow after it has been closed.  How do I do that?  

Also, I would like the closed menu items to shrink in height to  18px when closed with the open menu height of being 28px.  I have tried the code below but it just makes every menu item shrink to 18px and not keep the open one 28px.  Please help.  


$(document).ready(function () {
            $('LI.drawer UL').hide(); // hide all ULs inside LI.drawer 
            $('H2.drawer-handle').click(function () {
                $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");  // open selected menu item and mark it
                $(".MarkClosed").css({ height: "28px" });
                $("H2.drawer-handle").css({ height: "18px" });
                $(this).not(".MarkClosed").addClass('open').next().slideDown();  // open selected menu item if it has not been marked
                $(".MarkClosed").removeClass("MarkClosed"); // if marked close and remove marked
                $(this).not(".MarkClosed").find('IMG').attr("src", "RightArrow.gif");
                $(this).not(".MarkClosed").find('IMG').attr("src", "DownArrow.gif");

            });
        });

Open in new window

0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33659554
Hi, use the below code:
$(document).ready(function () {
	$('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
	$('H2.drawer-handle').click(function () {
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
		$(this).not(".MarkClosed").addClass('open').next().slideDown();
		$('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
		$(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
		$(".MarkClosed").removeClass("MarkClosed");
	});
});

Open in new window

0
 
LVL 15

Accepted Solution

by:
StealthyDev earned 2000 total points
ID: 33659609
And I would like to give you the script for increasing/decreasing the height as per your requirement.

Please note that your font size is 20px and you also have some padding, so shrinking to 18px is not possible. I have given an example of having shrink-text in 45px and expanded text in 55px..

Regards.
$(document).ready(function () {
	$('LI.drawer UL').hide(); // hide all ULs inside LI.drawer except the first one
	$('H2.drawer-handle').click(function () {
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("MarkClosed");
		$(this).not(".MarkClosed").addClass('open').next().slideDown();
		$('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
		$(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
		$('H2.drawer-handle').css("height", "45px");
		$(this).not(".MarkClosed").css("height", "55px");
		$(".MarkClosed").removeClass("MarkClosed");
	});
});

Open in new window

0
 
LVL 1

Author Closing Comment

by:upchelpdesk
ID: 33664263
Very helpful easy to follow solution, thanks a lot.  
0
 
LVL 1

Author Comment

by:upchelpdesk
ID: 33665229
Ok, one more question, When I close them menu item, I need the whole menu to return to its orginal height.  So if the menu height is 38px per item, and the on click height when I menu is open is 28 px for that item and the closed items shrink to 18px, if the active menu is them closed I need all menu items to go back to 28 px.  

How do I do that?  
0
 
LVL 15

Expert Comment

by:StealthyDev
ID: 33669183
Hi, I am not sure what you are looking for.. But I assume:
Heights: 45px-55px-65px
1. If an item is open its height should be 65px
2. If an item is closed, its height should be 45px
3. If an item is neither open, nor closed, its height should be 55px (default)
4. If all the items are closed, all height should come back to default (55px)

Update below stuff to achieve it !!!

Your new script:
      $(document).ready(function () {
            $('LI.drawer UL').hide();
            $('H2.drawer-handle').click(function () {
                  $('LI.drawer UL:visible').slideUp().prev().removeClass('open').addClass("closed").addClass("MarkClosed");
                  $('H2.drawer-handle').find('IMG').attr("src","Aiga_rightarrow.gif");
                  
                  $(this).not(".MarkClosed").addClass('open').next().slideDown();
                  $(this).not(".MarkClosed").find('IMG').attr("src","Aiga_leftarrow.gif");
                  
                  if($(this).not(".MarkClosed").html() == null)
                        $('H2.drawer-handle').removeClass('closed');
                        
                  $(".MarkClosed").removeClass("MarkClosed");
            });
      });

Update your styles:
    H2.drawer-handle {
        margin: 0;
        padding: 10px;
        background-color: #BAB1AD;
        height: 55px;
        margin-bottom: 3px;
        list-style: none;
            border: 1px solid red;
    }
   
    H2.drawer-handle.open {
        background-color: #BAB1AD;
        list-style: none;
            height: 65px;
            border: 1px solid blue;
    }
      
      H2.drawer-handle.closed {
            height: 45px;
            border: 1px solid green;
    }

Remove "open" from here:
<H2 class="drawer-handle open"><IMG src="Aiga_rightarrow.gif" style="height:10px;width:10px;"/>     WORSHIP</H2>
<H2 class="drawer-handle"><IMG src="Aiga_rightarrow.gif" style="height:10px;width:10px;"/>     WORSHIP</H2>

Best Regards.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question