Include the script and CSS files on your page:
<!-- jQuery --><script src="jquery-1.4.4.min.js" type="text/javascript"></script><!-- SmartMenus jQuery plugin --><script src="jquery.smartmenus.js" type="text/javascript"></script><!-- SmartMenus core CSS (required) --><link href='sm-core-css.css' rel='stylesheet' type='text/css' /><!-- "sm-blue" menu theme (optional, you can use your own CSS, too) --><link href='sm-blue/sm-blue.css' rel='stylesheet' type='text/css' />
The menu definition is a standard unordered list structure:
<ul id="main-menu" class="sm sm-blue"> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a> <ul> <li><a href="#">Item 2-1</a></li> <li><a href="#">Item 2-2</a></li> <li><a href="#">Item 2-3</a></li> </ul> </li> <li><a href="#">Item 3</a></li></ul>
You always need to set the sm
class and, optionally, if you would like to use some of the default themes, set it's class too (e.g. like sm-blue
in the example above).
If you would like to have a vertical main menu instead of horizontal, you also need to set the sm-vertical
class (and sm-blue-vertical
if you use the "sm-blue" theme):
<ul id="main-menu" class="sm sm-vertical sm-blue sm-blue-vertical"> ...
The script is initialized like any other jQuery plugin:
$(function() { $('#main-menu').smartmenus();});
collapsibleHideFunction: null
collapsibleHideFunction: function($ul, complete) { $ul.SlideUp(250, complete); }
$ul
(the sub menu).collapsibleShowFunction: null
collapsibleShowFunction: function($ul, complete) { $ul.SlideDown(250, complete); }
$ul
(the sub menu).hideFunction: null
hideFunction: function($ul, complete) { $ul.fadeOut(250, complete); }
$ul
(the sub menu).popupShow
and popupHide
API methods. Make sure you position offscreen or hide with display:none
in your CSS any popup menus you have.highlighted
class to the links (the A elements).subMenusSubOffsetX
option, too.subMenusSubOffsetY
option, too.current
class to the A element of the item linking to the current page URL.current
class to the A elements of all ancestor items of the current item. This option matters only if markCurrentItem: true
span.sub-arrow
).showFunction: null
showFunction: function($ul, complete) { $ul.fadeIn(250, complete); }
$ul
(the sub menu).'prepend'
, or 'append'
.mainMenuSubOffsetX
option, too.mainMenuSubOffsetY
option, too.return false
and the item's sub menu will not be shown.$('#main-menu').bind('activate.smapi', function(e, item) {});
return false
and the sub menu will not be shown.$('#main-menu').bind('beforefirstshow.smapi', function(e, menu) {});
return false
and the sub menu will not be hidden.$('#main-menu').bind('beforehide.smapi', function(e, menu) {});
return false
and the sub menu will not be shown.$('#main-menu').bind('beforeshow.smapi', function(e, menu) {});
$('#main-menu').bind('blur.smapi', function(e, item) {});
return false
and the item will not be selected (i.e. it's link won't be loaded) and if there is a sub menu which should appear on click, it won't be shown, too. This event is fired even for disabled items so you may want to check the select
event, too.$('#main-menu').bind('click.smapi', function(e, item) {});
$('#main-menu').bind('focus.smapi', function(e, item) {});
$('#main-menu').bind('hide.smapi', function(e, menu) {});
$('#main-menu').bind('mouseenter.smapi', function(e, item) {});
$('#main-menu').bind('mouseleave.smapi', function(e, item) {});
return false
and the item will not be selected (i.e. it's link won't be loaded). This event is not fired for disabled items so you may want to check the click
event, too.$('#main-menu').bind('select.smapi', function(e, item) {});
$('#main-menu').bind('show.smapi', function(e, menu) {});
The following methods are global for all menu instances.
$.SmartMenus.destroy();
$.SmartMenus.hideAll();
The following methods are available for each menu instance.
$('a#myItem')[0].blur();
$('#main-menu').smartmenus('destroy');
true
is passed, no overlay will be displayed over the main menu.// disable the menu$('#main-menu').smartmenus('disable');// disable the menu but don't display an overlay over the main menu$('#main-menu').smartmenus('disable', true);
$('#main-menu').smartmenus('enable');
itemActivate
first to make sure the menu containing the item is visible when you try to focus it, otherwise the item might not be focused by the browser.$('a#myItem')[0].focus();
focus
method.// activate the item$('#main-menu').smartmenus('itemActivate', $('a#myItem'));// activate the item and focus it$('#main-menu').smartmenus('itemActivate', $('a#myItem'));$('a#myItem')[0].focus();
$('#main-menu').smartmenus('menuHideAll');
isPopup: true
option is set. When isPopup: false
the main menu is permanent (always visible) and cannot be hidden with this method.true
is passed, the menu will be hidden immediately, otherwise the hideTimeout
option specifies the delay before hiding the popup menu.// hide a popup menu$('#popup-menu').smartmenus('popupHide');// hide a popup menu immediately without a delay$('#popup-menu').smartmenus('popupHide', true);
isPopup: true
option is set. When isPopup: false
the main menu is permanent (always visible) and cannot be shown with this method.left
declaration to be set for the popup menu before it is shown.top
declaration to be set for the popup menu before it is shown.// show the popup at left:100px;top:100px;$('#popup-menu').smartmenus('popupShow', '100px', '100px');// show the popup right below some target elementvar $targetElm = $('#targetElm'), targetOffset = $targetElm.offset();$('#popup-menu').smartmenus('popupShow', targetOffset.left, targetOffset.top + $targetElm.outerHeight());
innerHTML
of the root UL element.var $menu = $('#main-menu');// append a new main menu item$menu.append('<li><a href="#">New item</a></li>');// add a sub menu with 3 items to the new main menu item$menu.children('li:last').append('<ul> <li><a href="#">New item</a></li> <li><a href="#">New item</a></li> <li><a href="#">New item</a></li></ul>');// refresh the menu after the DOM operations$menu.smartmenus('refresh');
You can style the menus and items the same way you can style any other unordered list on your page. The UL
elements represent the menu boxes and the A
elements - the menu items.
You always need to include on your pages the SmartMenus core CSS sm-core-css.css
which includes just some basic rules that take care of things like resetting default list styles, main menu items arrangement, etc. And then you can either use (and modify to suit your needs) some of the default themes, or you can even use your own CSS to style the menus and items as you like.
There are some classes that you can and may need to set to your main menu (the root UL
element):
<ul id="main-menu" class="sm"> ...
<ul id="main-menu" class="sm sm-vertical"> ...
If you use some of the default menu themes, you will also need to set its class too - e.g. sm-blue
and possibly sm-blue-vertical
for the default blue theme.
There are some classes that you can set or are set automatically by the script to the menu items (the A
elements):
<li><a href="#" class="disabled"> ...
markCurrentItem
and markCurrentTree
options.keepHighlighted: true
option is set, the script will automatically add this class at runtime to any item that has its sub menu displayed. The class is removed as soon as the sub menu is hidden.Both touch and mouse input are supported by default. You don't need to do anything about it - your menus will just work and respond to whatever input is available on the device. The script even supports simultaneous touch and mouse input (for example, an Android device with a USB mouse plugged in, a Windows 8 touch-enabled device, etc.) which means that the menus will respond properly to the mouse cursor movement and any touches at the same time.
By default the script supports small-screen devices by transforming any horizontal or vertical main menu with drop-down sub menus into a vertical menu bar with collapsible sub menus when the browser viewport is not wide enough.
You can control in which cases (e.g. at what width) this should happen (and should it happen at all) by using CSS media queries like you do normally for your responsive page layouts. The default themes are all optimized for small screen devices and any menus that use these themes will be automatically transformed to collapsible menus at given viewport widths. For example, the default blue theme includes the following CSS:
@media screen and (max-width: 640px) { /* The following will make the sub menus collapsible for small screen devices (it's not recommended editing these) */ ul.sm-blue{width:auto !important;} ul.sm-blue ul{display:none;position:static !important;top:auto !important;left:auto !important;margin-left:0 !important;margin-top:0 !important;width:auto !important;min-width:0 !important;max-width:none !important;} ul.sm-blue>li{float:none;} ul.sm-blue>li>a,ul.sm-blue ul.sm-nowrap>li>a{white-space:normal;} ul.sm-blue iframe{display:none;} /* more rules to style the collapsible menu bar ... */}
These rules are basically what is required. When you use these, the script will automatically detect when the main menu should become collapsible and take care of everything. What you may want to edit in the above media query is the max-width: 640px
part where you specify the exact width break point.
If you are not using any of the default themes (i.e. you are styling your menus from scratch) and would like to make your menus collapsible on narrow viewports, you will just need to use the above CSS - just replace ul.sm-blue
with the class you use or even with an id selector for your root UL element:
To use the script with right-to-left text you just need to set the sm-rtl
class to the root UL element:
<ul id="main-menu" class="sm sm-rtl"> ...<!-- or --><ul id="main-menu" class="sm sm-vertical sm-rtl"> ...
and the script will take care of everything.
Here is some sample code that adds a menu toggle show/hide button on small screens.
HTML:
Add the collapsed
class to the root UL element:
<ul id="main-menu" class="sm collapsed"> ...<!-- or --><ul id="main-menu" class="sm sm-vertical collapsed"> ...Add the menu button code on your page:
<a id="menu-button"></a>
CSS:
#menu-button { display:none; /* style it as you like... */}#menu-button:before { content:'Menu -';}#menu-button.collapsed:before { content:'Menu +';}@media screen and (max-width: 640px) { /* show the button on small screens */ #menu-button { display:inline-block; } /* hide the menu when it has the "collapsed" class set by the script */ #main-menu.collapsed { display:none; }}
JavaScript:
$(function() { $('#menu-button').click(function() { var $this = $(this), $menu = $('#main-menu'); if (!$this.hasClass('collapsed')) { $menu.addClass('collapsed'); $this.addClass('collapsed'); } else { $menu.removeClass('collapsed'); $this.removeClass('collapsed'); } return false; }).click();});
This will show a menu toggle button (link) when the viewport width is less than 640px. This is, of course, just a basic example - you can tweak the styling of the button as you like, change the exact width breakpoint, etc.
The script requires and fully supports jQuery 1.4.4+.
聯(lián)系客服