Code Monkey home page Code Monkey logo

offcanvasmenu's Introduction

offCanvasMenu

offCanvasMenu is a jQuery/Zepto plugin that provides an easy way to implement an off-canvas toggling menu, a navigation metaphor made popular by mobile applications.

When activated, offCanvasMenu "slides" the menu element into view, "pushing" other content to the side.

Setup

1. Include jQuery (or Zepto)

Our example comes with jQuery 1.9.1 (but 2.0.0 should work as well).

<script src="lib/jquery-2.0.0.min.js"></script>

If you'd prefer, you can use Zepto instead.

<script src="lib/zepto-1.0.min.js"></script>

2. Include Modernizr (Optional)

If you're using jQuery but want CSS transitions (Super swank! So much prettier!), you'll need Modernizr. Our example comes with a custom build that only contains the tests needed.

<script src="lib/modernizr.custom.js"></script>

You can check out our Modernizr build details.

Needed tests (if you want to add to an existing Modernizr build):

  • csstransforms
  • csstransitions

Note: Modernizr is optional (and not at all necessary if you're using Zepto). If you don't include it, the plugin will fall back to JS animations.

3. Include the plugin

<script src="lib/jquery.offcanvasmenu.js"></script>

4. Initialize your menu!

$.offCanvasMenu();

Options

$.offCanvasMenu({
  direction : 'left',
  coverage  : '70%',
  trigger   : '#menu-trigger',
  menu      : '#menu',
  duration  : 250,
  container : 'body',
  classes   : {
    inner    : 'inner-wrapper',
    outer    : 'outer-wrapper',
    container: 'off-canvas-menu',
    open     : 'menu-open'
  },
  transEndEventNames: {
    'WebkitTransition' : 'webkitTransitionEnd',
    'MozTransition'    : 'transitionend',
    'OTransition'      : 'oTransitionEnd otransitionend',
    'msTransition'     : 'MSTransitionEnd',
    'transition'       : 'transitionend'
  }
});

Options you may wish to change

  • direction: (string) Direction from which the menu enters the containing element. Valid values are left or right. Default left.
  • coverage: (string) Width (in CSS units) of the menu when it is open/active. Relative units are relative to the container element. In all but the most experimental cases this is the body element, which means this effectively translates to coverage of the visible viewport. px or other non-percentage units are OK, but you must include the unit. Default is 70%. We haven't tested with much variation to that!
  • trigger: jQuery selector for the element that should trigger the show/hide of the menu. Default #menu-trigger.
  • menu: jQuery selector for the menu element itself. Default #menu.

Other options

For the most part, you'll want to leave these alone; they're there in case you run into namespace conflicts in CSS or other deeper issues.

  • duration: The time the animation should take to complete in milliseconds.
  • container: Nominally it should be possible to use a different container element other than the body element that is the default. But we haven't tried it!
  • classes : The class names that get assigned to different elements needed to make the menu work. You can change these if you have a conflict or other burning desire for change.
  • transEndEventNames: When CSS transitions are used we attach some events to the transitionend callback, which can differ in name browser-to-browser. We use a method similar to Modernizr and Twitter Bootstrap for determining the event name, referencing the keys in this list.

5. Functions

  • on(): You'll usually run this function right after initializing the menu. It causes the menu to be moved off-canvas and enables toggle(), show(), and hide().
  • off(): This function causes the menu to move to its original position and disables toggle(), show(), and hide().
  • toggle(): Toggles the menu.
  • show(): Shows the menu.
  • hide(): Hides the menu.

Known Issues

Android animation bugs

Some versions of the Android browser handle percentage transforms rather strangely. Specifying a non-percentage value for coverage should alleviate the issue.

License

Released under the MIT License.

This repository contains other libraries that may or may not fall under the same license:

offcanvasmenu's People

Contributors

bradleystaples avatar danielruf avatar mattpage avatar stevenlangbroek avatar tylersticka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

offcanvasmenu's Issues

Clicking on content when menu is open should close the menu

I think it would be an enhancement, if clicking on content when menu is open would close the menu. This is the known behavior from Facebook. Currently the only way to close the menu is to click on the trigger again.

Optionally swiping could also close and maybe even open the menu. This is also the behavior known from Facebook.

I think providing options for each of these behaviors would be very nice.

Chrome for iOS flicker

When you scroll (start scrolling or change scroll direction), the background of the off canvas menu flickers white

Video: http://youtu.be/UVZtGnF4D6E

This exists both in the hosted demo & within my project. Does not exist in Safari.

Prevents listening to window resizing with other scripts

In the actions.hide method, window resizing is done as

$(window).off("resize")

instead of

$(window).off("resize", actions.setHeights)

Is there a reason for this? It's preventing other window resizing events from being listened to for other reasons. I added the actions.setHeights and don't seemingly have any issues but didn't want to submit a pull request without seeing if there was some reason for the universal off("resize").

Menu hash conflicts in IE9

The hash change stuff introduced in response to #5 have broken some of the menu functionality in IE9. When closing the menu in the demo, the hash reappears and causes visual and interaction issues.

First menu item inadvertently clicked when toggling menu in demos on iOS

@mattg Well this is weird... as of the most recent version of the demos (both simple and responsive), when I tap the menu trigger, the phone also taps the first item of the menu as soon as it opens.

I experienced this without fail this evening on my iPhone 5 running the latest version of iOS 6.

Out of curiosity, I reverted my fix for #9 but the problem remains.

Resolution Testing

Is there a way to configure the code to meet a screen resolution max of 768 pixels in the code somewhere?

Menu Flickering

I love the simplicity of this menu, however, I am experiencing a flickering issue. When I open the menu, I notice a white flicker on the side of the menu. Additionally, in some cases the menu disappears entirely.

Any recommendations on how to solve this issue?

Question: Keeping trigger and menu position fixed?

Question that I've been struggling with a little bit: is there a good way to keep the menu and trigger fixed as a used scrolls through a page? Several CSS hacks/attempts later, I'm not getting extremely reliable results.

Occasional Fails on menu show after resize in responsive demo

First show of menu after resizing sometimes fails (in Chrome on OS X). Fail state is that the menu slides in but then either "disappears" completely (leaving a blank space where the menu element should display) or doesn't set its height correctly (menu element is only as "tall" as the three links in it).

2 Off Canvas side bars?

Hi,
Is it possible to have two Off Canvas side bars in the same page? One for the left and another for the right just like in Facebook app.

Thanks

Fixed menu at top?

This could be a feature. As i scroll down the page, it is possible to have the menu always visible and on top. For long content on mobile, its not efficient to scroll a long way up to access the menu.

thanks

IE7 support?

Hi,

I heard about this plugin on the shop talk show. It seems great and lightweight, but I need IE7 support on an upcoming project I'd like implement an offcanvas menu on. I gave the demo page a quick whirl in BrowserStack and the menu fires but there is no content in it for some reason.

I was just wondering if you guys knew about this and/or had any ideas of how to fix?

Cheers

Not changing display type when turning on

I was wanting to use this with a menu which was hidden by default (in the stylesheet), on a responsive site, as the plugin did not toggle the display state with the css upon turning on.

Although if the menu wasn't hidden by default within the stylesheet, although when turning the oCM on, the menu would hide, there would be a flicker whilst the element was hidden.

I rectified this locally, by simply adding the property 'display: block' to the script where it adds the styles to the document (Line 48 in 'jquery.offcanvasmenu.js).

baseCSS = "<style>  " + outer + " {      left: 0;      overflow-x: hidden;      position: absolute;      top: 0;      width: 100%;    }    " + inner + " {      position: relative;    }    " + container + " " + settings.menu + " {      height  : 0;      left    : " + menuLeft + ";      margin  : 0;      overflow: hidden;      position: absolute;      top     : 0;      width   : " + settings.coverage + ";  display: block;  }  </style>";

This rectified the problem, though I am sure there may be a better way to prevent this, or allow for hidden elements to be used as menus.

Account for scenario where JS or menu is not enabled in demo

Currently the demo page falls back fairly gracefully when the menu is disabled, but the menu button becomes inactive.

It would be nice if the menu button were an anchor with an href value of #menu so it could still have some semantics in lieue of full functionality.

Responsive demo

One of the cool things about this plugin is its ability to activate and deactivate dynamically. It would be great to have a responsive demo (perhaps re-labeling the existing one as "simple") to show off this scenario.

Lingering elements when menu is deactivated

When the plugin is instantiated, it wraps the body elements in two div elements.

Ideally this wouldn't happen until on is fired, and would be removed when off is fired. As-is, CSS issues relating to inheritance can crop up when the elements are left around.

Menu sliding issues

Hi,

Please see new.creditsolutions.ca. When you click the menu button to open the menu, the menu slides very quickly rather than easing in. When you click the menu button to close the menu, it slides slower, but after it is closed there is a flash of the menu that happens. This is on an iPhone 4.
On the 3GS the opening and closing of the menu is very glitchy and doesn't look natural at all. It kind of flashes closed.
On a Samsung, the menu opening animation time is very sudden. However the closing is very natural, with the expected easing.

Is there a way to fix these issues?

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.