> Slide-in menu

slidein-menu

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.


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

/Share the joy

/Reactions

    • Author: Bobby
    • Posted on: 2010/05/25
    • At: 17:43

    Really Awesome!

    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).

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

      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…. :-(

    Reply
    • Author: John
    • Posted on: 2010/05/26
    • At: 21:12

    Nice, but you cannot scroll the page with this?

    Reply
      • Author: John
      • Posted on: 2010/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.

      Reply
        • Author: Selcuk
        • Posted on: 2011/10/25
        • At: 21:17

        I’ve combined the two plus ‘Add to home screen’; all by Matteo for a webapp i’m running. works like a charm. Thanks Matteo.

    • Author: ade
    • Posted on: 2010/05/31
    • At: 10:48

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

    Reply
    • Author: Steve
    • Posted on: 2010/05/31
    • At: 13:57

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

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

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

    (but no HW acceleration)

    Reply
    • Author: Bjorn
    • Posted on: 2010/06/07
    • At: 16:47

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

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

      Reply
      • Author: Bridget Stewart
      • Posted on: 2010/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.

      Reply
    • not yet, sorry. Will do asap

      Reply
        • Author: MonkeyMagic
        • Posted on: 2011/11/09
        • At: 02:45

        Hey, I still cannot find a demo of this ?

        • Author: Martin
        • Posted on: 2011/11/15
        • At: 02:00

        HI there!
        Matteo thanks for this cool scripts. You will be my hero if you can provide an example of MenuSlider & iScroll working together.

        Thanks again.

    • Author: max
    • Posted on: 2010/06/11
    • At: 22:10

    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)

    Reply
      • Author: Mike
      • Posted on: 2010/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?

      Reply
    • Author: Mike
    • Posted on: 2010/06/12
    • At: 17:49

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

    Reply
      • Author: David
      • Posted on: 2010/06/13
      • At: 12:04

      Yes it works. A little tricky but works great.

      Reply
    • Author: David
    • Posted on: 2010/06/13
    • At: 12:04

    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.

    Reply
      • Author: Erik
      • Posted on: 2010/10/14
      • At: 16:12

      @Matteo: iscroll and slide-in both work great but I can not get them to work together? Do you have an example of a “carousel” in a slide-in menu?

      @David: you were able to get it to work on the iPhone simulator? Do you have example code available?

      Thx. E.

      Reply
      • it’s pretty simple to have iscroll inside a slide-in menu. I’ll try to post a demo as soon as possible.

    • Author: dave
    • Posted on: 2010/06/15
    • At: 01:02

    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]

    Reply
    • Author: dave
    • Posted on: 2010/06/15
    • At: 22:16

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

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

      Reply
    • Author: Leon
    • Posted on: 2010/06/16
    • At: 18:00

    I love u (in a man-ly way)

    Reply
    • Author: H4CK3R
    • Posted on: 2010/06/19
    • At: 21:54

    Brilliant work!!!
    So many great projects!!

    Reply
    • Author: Dominic
    • Posted on: 2010/06/21
    • At: 00:53

    great work!! keep it up

    Reply
    • Author: daniel
    • Posted on: 2010/07/04
    • At: 10:06

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

    thanks

    Reply
    • Author: Borut Tomažin
    • Posted on: 2010/07/23
    • At: 13:09

    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?

    Reply
      • Author: Borut Tomažin
      • Posted on: 2010/07/23
      • At: 13:11

      Forget to mention that I run iPhone OS 3.1…

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

      Reply
    • Author: Borut Tomažin
    • Posted on: 2010/07/23
    • At: 13:47

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

    Reply
      • Author: Borut Tomažin
      • Posted on: 2010/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?

      Reply
      • Author: Borut Tomažin
      • Posted on: 2010/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!

      Reply
    • Author: Neil
    • Posted on: 2010/08/09
    • At: 14:32

    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?

    Reply
    • Author: Joop
    • Posted on: 2010/08/13
    • At: 17:18

    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.

    Reply
    • Author: Iain Munro
    • Posted on: 2010/08/19
    • At: 17:08

    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

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

      Reply
    • Author: Steve W
    • Posted on: 2010/08/25
    • At: 16:54

    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.

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

      Reply
      • Author: s0b
      • Posted on: 2010/09/06
      • At: 16:55

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

      Thanks Matteo!

      Reply
      • Author: egfx
      • Posted on: 2010/09/08
      • At: 09:03

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

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

      • Author: Erik
      • Posted on: 2010/10/14
      • At: 16:21

      Sliding in from the left is not that difficult to achieve. Simply make some changes to the javascript and to the css. Basically it is changing widths with heigths and pageY becomes pageX. Below changes that should to the trick.

      #slidedownmenu {
      position:absolute;
      height:100%;
      width:200px;
      top:0px;
      left:-200px;
      }

      #slidedownmenu .handle {
      position:absolute;
      left:200px;
      top:0;
      width:28px;
      height:100%;
      }

      And in the javascript (only part with the required changes):

      function slideInMenu (el, opened) {
      this.container = document.getElementById(el);
      this.handle = this.container.querySelector(‘.handle’);

      this.openedPosition = this.container.clientWidth;

      this.container.style.opacity = ’1′;
      this.container.style.left = ‘-’ + this.openedPosition + ‘px’;
      this.container.style.webkitTransitionProperty = ‘-webkit-transform’;
      this.container.style.webkitTransitionDuration = ’400ms’;

      if ( opened===true ) {
      this.open();
      }

      this.handle.addEventListener(‘touchstart’, this);
      }

      slideInMenu.prototype = {
      pos: 0,
      opened: false,

      handleEvent: function(e) {
      switch (e.type) {
      case ‘touchstart’: this.touchStart(e); break;
      case ‘touchmove’: this.touchMove(e); break;
      case ‘touchend’: this.touchEnd(e); break;
      }
      },

      setPosition: function(pos) {
      this.pos = pos;
      this.container.style.webkitTransform = ‘translate3d(‘ + pos + ‘px,0,0)’;

      if (this.pos == this.openedPosition) {
      this.opened = true;
      } else if (this.pos == 0) {
      this.opened = false;
      }
      },

      touchStart: function(e) {
      e.preventDefault();
      e.stopPropagation();

      this.container.style.webkitTransitionDuration = ’0′;
      this.startPos = this.pos;
      this.startDelta = e.touches[0].pageX – this.pos;

      this.handle.addEventListener(‘touchmove’, this);
      this.handle.addEventListener(‘touchend’, this);
      },

      touchMove: function(e) {
      var delta = e.touches[0].pageX – this.startDelta;

      if (delta this.openedPosition) {
      delta = this.openedPosition;
      }

      this.setPosition(delta);
      },

      Reply
      • Author: Francisco lopez lecube
      • Posted on: 2011/10/12
      • At: 19:53

      Slide from left or right would be really nice since at least on ipad, when you are holding it its much more confortable to have menus on the sides because of the position of the hands.

      Reply
  • Great work, cheers!
    Do you have some sort of public repo for this (like Google code)?

    Reply
    • not yet, sorry. I’ll do it when the new version will be ready.

      Reply
    • Author: Marc
    • Posted on: 2010/10/26
    • At: 11:06

    Congratulations for your work !!

    I’m using for a Ipad version of a website, slide-in menu AND iScroll in the same script.
    Sometimes, when the page loads, the menu is locked and not reverse its position in “closed”.

    Have you any idea?

    My code :
    var myScroll;
    var menu;
    function loaded() {
    menu = new slideInMenu(‘slidedownmenu’, false);
    menu.close();
    myScroll = new iScroll(‘scroller’, {
    snap:true,
    momentum:false,
    hScrollbar:false,
    onScrollEnd: function () {
    document.querySelector(‘#indicator > li.active’).className = ”;
    document.querySelector(‘#indicator > li:nth-child(‘ + (this.pageX+1) + ‘)’).className = ‘active’;
    //alert(this.pageX);
    //if (this.pageX == 4) document.location=’de.php’;
    }
    });
    document.addEventListener(‘touchmove’, function(e){ e.preventDefault(); e.stopPropagation(); });

    }

    document.addEventListener(‘DOMContentLoaded’, loaded, false);

    Thank you very much.

    Reply
    • Author: David
    • Posted on: 2010/11/02
    • At: 15:44

    Hi thx for the great script!

    Q: Is there a way to use more than one slide in menu? I’m having problems with my second one. First works perfect but second slide in menu (different ID of course) is not slideable and open by default.

    Reply
    • they probably overlap

      Reply
      • Author: David
      • Posted on: 2010/11/02
      • At: 19:53

      Not quite. I noticed that the push each other down so only the first is functioning. The second is pushed by its size (notice that all slideinmenu-divs have the same size) and the third by twice its sice. Is there a way to position the div layer so that each they don’t push each other down but are still functioning.

      Info:
      I’m using jQTouch to switch between panels (main div layers) in a single html file.

      I appreciate your help.

      Reply
    • Author: Mike
    • Posted on: 2010/11/15
    • At: 17:57

    This is great!

    Is there a way at present to get it to slide up from the bottom rather than the top of the screen?

    Reply
    • Author: Nathan
    • Posted on: 2011/01/28
    • At: 19:30

    Is there anyway of making it at the bottom?

    Reply
      • Author: kamila
      • Posted on: 2011/07/19
      • At: 14:01

      With few modifications you can get a bottom bar version.

      css:
      #footerToolbar {
      position:absolute; z-index:40;
      height:80px;
      bottom:-80px;
      }
      #footerToolbar .handle {
      position:absolute;
      z-index:90;
      height:40px;
      top:-40px;
      }

      this.openedPosition = -this.container.clientHeight;
      this.container.style.top = iModel.screenHeight + 'px';

      touchMove: function(e) {
      var delta = e.touches[0].pageY - this.startDelta;
      if (delta > 0) {
      delta = 0;
      } else if (delta < this.openedPosition) {
      delta = this.openedPosition;
      }
      this.setPosition(delta);
      }

      Reply
      • thanks for sharing!

        • Author: pol
        • Posted on: 2012/01/25
        • At: 20:31

        Hello,
        trying to fix it at the bottom with no luck, what’s iModel in
        this.container.style.top = iModel.screenHeight + 'px';

        vs the original

        this.container.style.top = '-' + this.openedPosition + 'px';

      • Author: kamila
      • Posted on: 2011/07/19
      • At: 14:02

      btw. great script :)

      Reply
    • Author: Ben
    • Posted on: 2011/02/12
    • At: 08:28

    Does this prevent safari from being able to scroll?

    I think there’s been a couple of comments addressing this, but they’ve been confused for requesting the iscroll functionality.

    I personally can’t figure out what’s going wrong and why I can’t scroll my site after installing this.

    Reply
    • Remove
      document.addEventListener(‘touchmove’, function(e){ e.preventDefault(); e.stopPropagation(); });

      and you’ll be able to scroll again.

      Reply
      • Author: Ben
      • Posted on: 2011/02/12
      • At: 17:46

      Thanks for your quick answer! Thought I was going to wake up and have to find a new solution.

      Love your work Matteo!

      And really appreciate your response time, I’ll definitely be referring people to your great site.

      Reply
    • Author: Ricks
    • Posted on: 2011/02/18
    • At: 08:00

    +1 for pinning to bottom

    Reply
    • Author: Ron
    • Posted on: 2011/04/11
    • At: 01:12

    Any tips on the best way to set it to the bottom? Thanks Matteo!

    Reply
    • Author: Moses
    • Posted on: 2011/04/14
    • At: 18:06

    How can I set the menu to be located on the bottom as a page “footer” and open upward? Is this possible?

    Reply
    • Author: Mark
    • Posted on: 2011/04/27
    • At: 05:50

    Based on Moses comment, it would be nice to have the menu on the footer. I will like to incorporate something like this in my app.

    Reply
    • Author: kenny
    • Posted on: 2011/05/10
    • At: 13:35

    Very good plugin…
    however, it seems that it is not supported in jQTouch… =[

    Reply
  • Thanks a lot for sharing. You have done a brilliant job. Your article is truly relevant to my study at this moment, and I am really happy I discovered your website. However, I would like to see more details about this topic.

    Reply
    • Author: jerry
    • Posted on: 2011/06/16
    • At: 22:41

    Hi there,

    i love this works here.
    unfortunable i dont find a right way to include iscroll in slide in :(
    is there a demo anywhere ? :)

    cheers Jerry

    Reply
    • Author: Korey
    • Posted on: 2011/06/20
    • At: 20:56

    Hi, this is great, I would love to use this in my footer.
    Also just a note about iOS5 compatibility. If you install this to your home screen you are then unable to make the pull down work as every time you make the pull down gesture from the top of your screen the new notifications pull down is triggered.
    As said, I cant wait for bottom positioning. Keep up the good work. Thanks!

    Reply
    • Author: AlexJ
    • Posted on: 2011/09/10
    • At: 01:08

    Love this… Thanks for making it Matteo!

    I have a quick question… Has anyone had any luck putting jquerymobile content on the slider? I’m able to use the slider on jquerymobile pages, however if I put a jqm element such as a nav bar, which I might add would be ideal, it freaks out and doesn’t place nice together…

    Anyone that might have a tip… Getting these two to play nicely together would be a huge help…

    Thanks,

    AlexJ

    Reply
    • Author: Jon
    • Posted on: 2011/10/13
    • At: 18:17

    Great code. Thank you!
    Is there any way I can get the slide menu not to automatically open when the page loads? I’d rather the user toggle it open if they wish.

    Cheers,

    JOn

    Reply
    • Author: Gregg
    • Posted on: 2011/10/18
    • At: 22:49

    I’m using this as a menu in conjunction with the iScroll snap and snap to elements.

    I’d love for the ability to click one of the menu items and have it auto-scroll to that LI

    Anyone know how to do this?

    Reply
    • Author: Andreas
    • Posted on: 2011/10/29
    • At: 13:58

    First off all, you did (and still do) an excellent job!!!

    If have made a “pseudo” app for ipad´s. I have a basic apple/app/frame with an external website to load in it. So I can open the app by a click on my ipad´s desktop.

    I´m using iSroll(4) with two scroll areas, a fix header and footer and a slideinmenu in the right top bar. I have put a simple link inside that menu, but its not clickable (???)

    Another question: I have a navbar on the left (iscroll) and a content on the right (iscroll and ajax). When I have scrolled down this content, how can I move to the top by clicking one of the next entries on the left?

    By the way: I have found a question from somebody who had problems with scolling in 2 bars. I solved the problem by increasing the value from 0 to 1000.


    function onCompletion () {
    setTimeout(function () { scroll1.refresh(); }, 1000);
    setTimeout(function () { scroll2.refresh(); }, 1000);
    }

    excuse my english ;-)

    Reply
      • Author: Andreas
      • Posted on: 2011/10/29
      • At: 14:35

      I´ve solved one:
      …I have put a simple link inside that menu, but its not clickable (???)…

      Reply
    • Author: MonkeyMagic
    • Posted on: 2011/11/09
    • At: 04:47

    2-way-iScroll and Slide in Menu ? I see a lot of people saying this can be done ?

    Any ideas how it can be done with the 2-way-iScroll ?

    Reply
    • Author: Thomas Blanc
    • Posted on: 2011/11/23
    • At: 15:25

    It works well on iPhone and Android, but i wanted to try on Opera Mobile and it doesn’t seems to run. Do you think it is easy to adapt the menu on it, or on Windows Phone (i’ve have not tested it yet)

    Reply
    • Author: BackwaterBob
    • Posted on: 2011/12/05
    • At: 21:02

    Really cool script.

    I tried to see if it could act as a hideable navigation for a traditional full-screen menu by changing the height to:

    #slidedownmenu {
    height:315px;
    top:-315px;
    }

    but then when you turn the phone to landscape mode, the handle falls to far below the frame.

    Similarly, when you use a percentage height like 90%, the effect still doesn’t translate to landscape mode because it remembers the old state.

    I would love to see the functionality of scalable menu heights introduced in a later version.

    Reply
    • Author: Jason Gonzales
    • Posted on: 2011/12/21
    • At: 15:59

    Matteo,
    I adapted your code a bit to make the menu slide in from the left. Thinking it might be a nice part of an options object to allow one to put the menu on any side. Would you be alright with me putting this on github, with your attribution still in the code?

    –J Gonzales

    Reply
    • go for it! the code is MIT licensed.

      Reply
    • Author: Lauren
    • Posted on: 2012/01/16
    • At: 22:48

    would this ever toggle properly in a regular browser? If so, it would be a miracle to incorporate into responsive sites.

    Reply
      • Author: Lauren
      • Posted on: 2012/01/18
      • At: 14:07

      duh… it’s easy enough to to add a toggle button in the visible part of the hidden menu. Thanks for this!

      Reply
    • Author: Sam
    • Posted on: 2012/01/29
    • At: 01:24

    This is fantastic. Has there been any headway on making it work from the footer? Would love this as a slideup menu. Thanks for your great work.

    Reply

/Leave a reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>