slidein-menuSometimes the screen of mobile devices is too small to fit all the controls, menus and options of your application. One solution is to create a slide in menu that the user can pull to access additional functions.
May 2010
25
Replies
43


Project info

Project state: ACTIVE (code is actively updated)
Last code update: 2010.05.28 – v0.1 beta 1
Device compatibility: iPhone/Ipod touch >=2.0, Android >1.6, iPad.
QR Code opens demo page. (On Android you may need to refresh the page once loaded, I’m looking into this issue with QR links).

Support development

If this script saved your day or you wish to support future developments you may consider sending some funds via PayPal.


Overview

Sometimes the screen of mobile devices is too small to fit all the controls, menus and options of your application. One solution is to create a slide in menu that the user can pull to access additional functions.

The menu is opened both by tapping and pulling the handle.

This widget is ridiculously simple, all it does is to slide in a hidden div on user’s request. Please note that the script is just a draft, I’ll keep developing it if there’s enough interest (see below).

How to use

Initialize the slide in menu on DOMContentLoaded:

menu = new slideInMenu('slidedownmenu');

slidedownmenu is the ID of the element to be slided.

The slider has to be absolutely positioned and the top:

#slidedownmenu {
  position:absolute;
  width:100%;
  height:115px;
  top:-115px;
  left:0;
  z-index:999;
}

The the example I set the top property to -115px so the menu is hidden while the page loads, but it’s not mandatory. The slideInMenu() function takes care of placing the div in the right position.

The handle is positioned to the bottom of the sliding layer. To make it stick out the initially closed menu you just need to set the bottom property equal to the negative value of the handle height:

#slidedownmenu .handle {
  -webkit-user-select:none;   /* user can't select */
  position:absolute;
  width:100%;
  height:20px;
  bottom:-20px;
  left:0;
}

Look at the source code of the online demo for details.

Syntax

The function accepts two parameters at startup:

slideInMenu(string ID, bool openAtStartup);

ID is the menu identifier. If you set openAtStartup to TRUE the menu will be in opened when initialized.

You can also open, close and toggle the menu programmatically.

var menu = new slideInMenu('slidedownmenu', true); // Initialize the menu and open it right away

menu.open();	// Open the menu

menu.close();	// Close the menu

menu.toggle();	// Toggle the menu open/close state

Future developments

  • Add Android compatibility
  • Programmatically open/close the menu
  • Should be possible to place the menu anywhere (ie: slide down/up/left/right)

Revisions history

v0.1 beta 1, 2010.05.28 – Added Android compatibility
v0.1 alpha 1, 2010.05.25 – Initial release

Reactions

  1. Bobby2010/05/25 at 17:43
    Reply

    Really Awesome!

     
  2. Sam (MosaLingua)2010/05/26 at 10:15
    Reply

    This is so great !
    Thanks a lot for sharing Matteo…

    I think I will use it in my next project : because It will work both in iPhone and Android, and it’s good for small screen device…

    Would be great to memorize the last position (provide a function to set it initially open or close at startup).

     
    • Matteo Spinelli2010/05/26 at 10:48

      Good point, I’ll add that feature to the to-do list.

       
  3. Marek Ventur2010/05/26 at 20:31
    Reply

    It’s nice but pretty buggy as a iPhone Web Apps because the whole touchable area is hidden behind the upper bar. It’s exactly the same size so you just can’t touch anything…. :-(

     
  4. John2010/05/26 at 21:12
    Reply

    Nice, but you cannot scroll the page with this?

     
    • John2010/05/26 at 21:21

      Would be cool if you can combine this and iscroll, and have this menu fixed at the bottom or top.

       
  5. ade2010/05/31 at 10:48
    Reply

    Love the idea but fails when u pinch zoom the page. Knicks your sub out of sync.
    Keep up the good work though

     
  6. Steve2010/05/31 at 13:57
    Reply

    Great concept. Doesn’t work on HTC Magic though.

    Nevertheless, I’ll probably look at implementing it in some sites I build somehow.

     
  7. Matteo Spinelli2010/05/31 at 14:10
    Reply

    To have it work on Android <1.6 change
    ‘translate3d(0,’ + pos + ‘px,0)’
    to
    'translate(0,' + pos + 'px)'

    (but no HW acceleration)

     
  8. Bjorn2010/06/07 at 16:47
    Reply

    This is a cool component. Unfortunate that you can not scroll the page though.

     
    • Matteo Spinelli2010/06/07 at 17:55

      I’ll try to make a demo page with both widgets working peacefully together

       
    • Bridget Stewart2010/08/25 at 18:10

      Have you set up a demo with both iScroll and SlideInMenu working together? I don’t seem to see it.

       
    • Matteo Spinelli2010/08/25 at 19:10

      not yet, sorry. Will do asap

       
  9. max2010/06/11 at 22:10
    Reply

    i found a bug. when in safari you add a bookmark on your homescreen and you open it in fullscreen mode the menu goes under the topbar :( (it happens in iscroll too)

     
    • Mike2010/06/12 at 17:44

      AWESOME components. I agree with Max regarding the menu goes under the topbar.

      Is page scrolling possible under the slide-menu?

       
  10. Mike2010/06/12 at 17:49
    Reply

    Or is it possible to combine iScroll function and slide-in-menu function together?

     
    • David2010/06/13 at 12:04

      Yes it works. A little tricky but works great.

       
  11. David2010/06/13 at 12:04
    Reply

    I was able to get it to work in iPhone simulator WITH iScroll. Wan’t able to get it to work on Droid. Some kind of width issue. Working on it.

    Great work.

     
  12. dave2010/06/15 at 01:02
    Reply

    Great work Matteo, please keep up your efforts developing UI components for mobile!

    I added preventDefault/stopPropagation to the touchMove and touchEnd events as well, otherwise the events would bubble up to the other touch events in my document.

    Line #29 this.handle.addEventListener(‘touchstart’, this);

    Is that right?? it seems to work fine, but shouldn’t ‘this’ be a function you want to bind to?

    In FF this causes:

    uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://domain.com/js/slideinmenu.js :: slideInMenu :: line 29" data: no]

     
  13. dave2010/06/15 at 22:16
    Reply

    adding a callback for open/close using the webkitTransitionEnd event would be a nice feature to have. :)

     
    • Matteo Spinelli2010/06/15 at 23:03

      very good advice. I’ll add it to the to-do

       
  14. Leon2010/06/16 at 18:00
    Reply

    I love u (in a man-ly way)

     
  15. H4CK3R2010/06/19 at 21:54
    Reply

    Brilliant work!!!
    So many great projects!!

     
  16. Dominic2010/06/21 at 00:53
    Reply

    great work!! keep it up

     
  17. daniel2010/07/04 at 10:06
    Reply

    good job, what do I have to change in order for me to put in the bottom without a scrollbar

    thanks

     
  18. pingback Quality Code « Mr Chris Jones2010/07/11 at 02:37
  19. Borut Tomažin2010/07/23 at 13:09
    Reply

    I could not get to run this great plugin on first generation of iPhone (e.g. 2G). On simulator it works great.

    Are you aweare of this problem?

     
    • Borut Tomažin2010/07/23 at 13:11

      Forget to mention that I run iPhone OS 3.1…

       
    • Matteo Spinelli2010/07/23 at 13:17

      Weird, it works on my old ipod touch (v3.1.2).

       
  20. Borut Tomažin2010/07/23 at 13:47
    Reply

    Yeah, it’s really wierd. I will on another iPhone.

     
    • Borut Tomažin2010/07/23 at 14:14

      I have done this way:

      var menu;

      function loaded() {
      document.addEventListener(‘touchmove’, function(e){ e.preventDefault(); e.stopPropagation(); });
      alert(“before”);
      menu = new slideInMenu(‘slidedownmenu’, true);
      alert(“after”);
      }
      document.addEventListener(‘DOMContentLoaded’, loaded);

      A load this with Safari on Mac but still no go. It comes to the “before” alert but not to the “after”.

      What could be wrong?

       
    • Borut Tomažin2010/07/23 at 14:18

      It loaded now ok. The problem was in JS path.
      Sory for this misunderstanding.

      By the way, this is great script. Go on like that!

       
  21. Neil2010/08/09 at 14:32
    Reply

    I can’t seem to get this to work with the JQTouch framework, seems the JQTouch CSS conflicts with the slide in menu…. any ideas?

     
  22. Joop2010/08/13 at 17:18
    Reply

    Would it be possible to combine this with your rotating wheel, so I could use it to navigate a site? Best would be from left to right and back.

     
  23. Iain Munro2010/08/19 at 17:08
    Reply

    Morning

    All your examples are great.

    My question on the Slidein code is:

    Does it work on the actual iPhone itself?

    I have tried and only get the page, but with no action.

    Iain

     
    • Matteo Spinelli2010/08/20 at 16:12

      Works for me. Try to refresh a couple of times. Also what iphone/OS version?

       
  24. Steve W2010/08/25 at 16:54
    Reply

    Hi Matteo,

    Great work. One question. Is it currently possible to have the menu slide in from the left? If not, what needs to be done to implement this?

    Cheers,

    Steve.

     
    • Matteo Spinelli2010/08/25 at 19:12

      not yet, but it’s in my to-do

       
    • s0b2010/09/06 at 16:55

      That can be a great feature, im waiting for it.

      Thanks Matteo!

       
    • egfx2010/09/08 at 09:03

      I second this feature. Whats the eta on a menu sliding in from the left or right?

       
    • Matteo Spinelli2010/09/08 at 09:13

      As soon as I get some spare time… very busy period, sorry.

       

Speak your mind