> Slide-in 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 compatibilityProgrammatically 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
Really Awesome!
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).
Good point, I’ll add that feature to the to-do list.
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….
Nice, but you cannot scroll the page with this?
Would be cool if you can combine this and iscroll, and have this menu fixed at the bottom or top.
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.
Love the idea but fails when u pinch zoom the page. Knicks your sub out of sync.
Keep up the good work though
Great concept. Doesn’t work on HTC Magic though.
Nevertheless, I’ll probably look at implementing it in some sites I build somehow.
To have it work on Android <1.6 change
‘translate3d(0,’ + pos + ‘px,0)’to
'translate(0,' + pos + 'px)'(but no HW acceleration)
This is a cool component. Unfortunate that you can not scroll the page though.
I’ll try to make a demo page with both widgets working peacefully together
Have you set up a demo with both iScroll and SlideInMenu working together? I don’t seem to see it.
not yet, sorry. Will do asap
Hey, I still cannot find a demo of this ?
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.
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)
AWESOME components. I agree with Max regarding the menu goes under the topbar.
Is page scrolling possible under the slide-menu?
Or is it possible to combine iScroll function and slide-in-menu function together?
Yes it works. A little tricky but works great.
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.
@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.
it’s pretty simple to have iscroll inside a slide-in menu. I’ll try to post a demo as soon as possible.
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]
adding a callback for open/close using the webkitTransitionEnd event would be a nice feature to have.
very good advice. I’ll add it to the to-do
I love u (in a man-ly way)
Brilliant work!!!
So many great projects!!
great work!! keep it up
good job, what do I have to change in order for me to put in the bottom without a scrollbar
thanks
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?
Forget to mention that I run iPhone OS 3.1…
Weird, it works on my old ipod touch (v3.1.2).
Yeah, it’s really wierd. I will on another iPhone.
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?
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!
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?
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.
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
Works for me. Try to refresh a couple of times. Also what iphone/OS version?
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.
not yet, but it’s in my to-do
That can be a great feature, im waiting for it.
Thanks Matteo!
I second this feature. Whats the eta on a menu sliding in from the left or right?
As soon as I get some spare time… very busy period, sorry.
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);
},
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.
Great work, cheers!
Do you have some sort of public repo for this (like Google code)?
not yet, sorry. I’ll do it when the new version will be ready.
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.
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.
they probably overlap
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.
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?
Is there anyway of making it at the bottom?
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);
}
thanks for sharing!
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';btw. great script
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.
Remove
document.addEventListener(‘touchmove’, function(e){ e.preventDefault(); e.stopPropagation(); });
and you’ll be able to scroll again.
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.
+1 for pinning to bottom
Any tips on the best way to set it to the bottom? Thanks Matteo!
How can I set the menu to be located on the bottom as a page “footer” and open upward? Is this possible?
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.
Very good plugin…
however, it seems that it is not supported in jQTouch… =[
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.
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
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!
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
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
set openAtStartup to false…
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?
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
I´ve solved one:
…I have put a simple link inside that menu, but its not clickable (???)…
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 ?
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)
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.
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
go for it! the code is MIT licensed.
would this ever toggle properly in a regular browser? If so, it would be a miracle to incorporate into responsive sites.
duh… it’s easy enough to to add a toggle button in the visible part of the hidden menu. Thanks for this!
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.