Solved

JQuery Slide Menu Slide Toggle on click

Posted on 2010-09-08
10
2,300 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
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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now