Another day, another jQuery Quick Tip. Today we have a piece of jQuery that will enable an end user to hide the Quicklaunch from view and free up some additional space if you have a lower resolution. As with my other Quick Tips the following code uses the OOTB v4.master so may not work if you have customised your design. (Changes to selectors should be all that would be required to get it working with your own design)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($) { var CookieName = "QuickLaunchHidden"; var MainArea = $('.s4-ca'); var MainAreaLeftMargin = $(MainArea).css('margin-left'); var NoLeftMargin = "0px"; var LeftArea = $('#s4-leftpanel'); var Collapse = "/_layouts/images/mewa_leftPage.gif"; var Expand = "/_layouts/images/mewa_rightPage.gif"; $(MainArea).prepend('<a class=\'min\' style=\'float:left; position:absolute; top:0px; left:0px;\' title=\'Hide \\ Show Quick Launch\'><img src=\''+ Collapse +'\'/></a>'); $('.min').click(function(){ //Toggle the Left Panel to hide it $(LeftArea).toggle(); $(LeftArea).is(":visible") ? MainArea.css('margin-left',MainAreaLeftMargin ) : MainArea.css('margin-left',NoLeftMargin); var img = $(this).children(); $(LeftArea).is(":visible") ? img.attr('src',Collapse) : img.attr('src',Expand ); }); }); </script> |
The script above is not much more complicated than previous ones but as always i will go through it piece by piece so you are clear as to what is going on. The first line is getting a hosted version of the jQuery library, obviously if you have this locally then it may be worth updating this reference appropriately.
|
var MainArea = $('.s4-ca'); var MainAreaLeftMargin = $(MainArea).css('margin-left'); var NoLeftMargin = "0px"; var LeftArea = $('#s4-leftpanel'); var Collapse = "/_layouts/images/mewa_leftPage.gif"; var Expand = "/_layouts/images/mewa_rightPage.gif"; |
The first six lines of code are to setup some variables that will be used during the rest of the script. We first get a reference to the main Right hand content div which in the case of the OOTB masterpage is the s4-ca classed div. Next we save the size of the left margin of this div so that it can be used later. We then set the LeftArea div for the item which will be hidden and also define the size of the left margin when collapsed which is what line three is for. Finally we have two variables that define the expand and collapsed images.
|
$(MainArea).prepend('<a class=\'min\' style=\'float:left; position:absolute; top:0px; left:0px;\' title=\'Hide \\ Show Quick Launch\'><img src=\'' + Collapse + '\'/></a>'); |
The next line is quite a long one but its nothing complicated. What we are doing is to the Defined MainArea we are adding our image with a hyperlink on it which is positioned at the top of the parent container.
|
$('.min').click(function(){ //Toggle the Left Panel to hide it $(LeftArea).toggle(); $(LeftArea).is(":visible") ? MainArea.css('margin-left',MainAreaLeftMargin ) : MainArea.css('margin-left',NoLeftMargin); var img = $(this).children(); $(LeftArea).is(":visible") ? img.attr('src',Collapse) : img.attr('src',Expand ); }); |
This final piece of code is the core of the script and contains the click event handler for our dynamically added code shown above. We first define the click handler, then we toggle the configured LeftArea so that it is hidden. Now if we stopped there we would have hidden the quicklaunch but the page would not expand to provide any more room. To fix this we then need to adjust the MainArea left-margin. Line four first checks if the left section is visible, and then if it is we set the MainArea margin-left to the configured NoLeftMargin otherwise we “reset” to the left-margin that was set on load. The final two lines updates the image to show the collapse or expand image depending on the status of the LeftArea.
That’s all there is to it. I hope like previous snippets that this will be useful for someone and if you have any recommendations or suggestions of new functionality that you would like to see then please let me know. As always your positive comments are always welcomed and keep me working on new content for you.