简单的侧边栏展开式菜单
XML/HTML Code
- <header>
- <div id="navbar">
- <a href="#" class="menubtn">menu</a>
- <!-- use captain icon for toggle menu -->
- <div id="hamburgermenu">
- <ul>
- <li><a href="#">Home</a></li>
- <li><a href="#">About</a></li>
- <li><a href="#">News</a></li>
- <li><a href="#">Products</a></li>
- <li><a href="#">Contact</a></li>
- </ul>
- </div>
- </div>
- <div class="overlay"></div>
- </header>
JavaScript Code
- $(function(){
- var menuwidth = 240; // 边栏宽度
- var menuspeed = 400; // 边栏滑出耗费时间
- var $bdy = $('body');
- var $container = $('#pgcontainer');
- var $burger = $('#hamburgermenu');
- var negwidth = "-"+menuwidth+"px";
- var poswidth = menuwidth+"px";
- $('.menubtn').on('click',function(e){
- if($bdy.hasClass('openmenu')) {
- jsAnimateMenu('close');
- } else {
- jsAnimateMenu('open');
- }
- });
- $('.overlay').on('click', function(e){
- if($bdy.hasClass('openmenu')) {
- jsAnimateMenu('close');
- }
- });
- function jsAnimateMenu(tog) {
- if(tog == 'open') {
- $bdy.addClass('openmenu');
- $container.animate({marginRight: negwidth, marginLeft: poswidth}, menuspeed);
- $burger.animate({width: poswidth}, menuspeed);
- $('.overlay').animate({left: poswidth}, menuspeed);
- }
- if(tog == 'close') {
- $bdy.removeClass('openmenu');
- $container.animate({marginRight: "0", marginLeft: "0"}, menuspeed);
- $burger.animate({width: "0"}, menuspeed);
- $('.overlay').animate({left: "0"}, menuspeed);
- }
- }
- });
原文地址:http://www.freejs.net/article_daohangcaidan_850.html