Right, time for a slightly more nerdy post. As I was looking for a simple way to make my jQTouch webapp iPad ready without having to resort to a fork (like Beedesk’s). I found the following solution that I’d like to share with you lot. The sollution is pretty simple, and is *unobtrusive*; it will not affect the iPhone/iPod layout and functionality (as far as I can tell).
Firstly, I ‘borrowed’ the section of Beedesk’s CSS that deals with the iPad layout. It basically provides a ‘splitscreen’ class and a ‘section=”aside”‘ parameter. I added one class for hiding the back buttons (and any other classes which would be of no use in the iPad layout) and saved it as an additional ‘ipad.css’:
1. ipad.css:
#jqt > [section="aside"] { -webkit-backface-visibility: hidden; -webkit-box-sizing: border-box; box-sizing: border-box; display: none; right: 0; min-height: 100%; } @media only screen and (min-device-width: 768px) { #jqt.splitscreen { width: 768px; margin-left: auto; margin-right: auto; -jqt-splitscreen: 1; } #jqt.splitscreen > * { left: auto; right: 0; width: 448px; border-radius: 5px; } #jqt.splitscreen > .unfixed { overflow-y: scroll; height: 100%; } #jqt.splitscreen > [section="aside"] { position: absolute; left: 0; right: auto; width: 320px; border-radius: 5px; } #jqt.splitscreen > :not([section="aside"]) { border-left: 1px solid; } } @media only screen and (min-device-height: 768px) and (orientation:landscape), only screen and (min-device-width: 1024px) { #jqt.splitscreen { width: 1024px; } #jqt.splitscreen > * { width: 704px; } #jqt.splitscreen > [section="aside"] { } }
Then I added a detection script to the index file which sets a flag when my app is being accessed from an ipad:
2. addition to the head section:
<script language="javascript" type="text/javascript"> // For use within normal web clients var isiPad = navigator.userAgent.match(/iPad/i) != null; // For use within iPad developer UIWebView // Thanks to Andrew Hedges! var ua = navigator.userAgent; var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua); </script>
Using this flag, I can now set the .css file to be loaded or not:
3. load .css if ipad:
<script language="javascript" type="text/javascript"> if (isiPad){ document.write("<style type="text/css" media="screen">@import "**path to your css files**ipad.css";</style>"); } </script>
(ofcourse, both these script can be merged in to 1 script call)
Then I modified my html code:
4. html code:
<div id="jqt" class="splitscreen">
and
<div id="home" section="aside">
‘home’ is my initial (current) div when in single column (iPhone/iPod) layout, containing the menu. Note that the class ‘current’ is missing. Hold on, I’ll get to that π
5. Now, the important bit
Based on the iPad flag, you should *dynamically* write the opening of the DIV you want to be visible in the right column so that it has *class ‘current’* when viewed on an iPad and *no class* when not viewed on an iPad. In my case ‘overzicht’ is the div I want to be displayed in the right column initially:
<script language="javascript" type="text/javascript"> if (isiPad){ document.write("<div id="overzicht" class="current">"); } else { document.write("<div id="overzicht">"); } </script>
*Note: Don’t bother writing jQuery addClass scripts instead of this, it will not work.*
Note 2: Make sure this div is somewhere *after* your home-div in the HTML.
At this point, when you ‘test drive’ your webapp, you will notice that the ‘current’ div will show up, but the left column remains empty. To fix this, and to *prevent the left column from ever animating* you just need to add 1 line of code to the initialisation section in jqtouch.js:
6. jqtouch.js modification:
// Determine what the "current" (initial) panel should be if ($('#jqt > .current').length == 0) { currentPage = $('#jqt > *:first'); } else { currentPage = $('#jqt > .current:first'); $('#jqt > .current').removeClass('current'); }
add this line right below that section: (line 909 at the time of writing, revision 166)
$('#home').addClass('current');
This will add the class ‘current’ to my home div (‘#home’) and place it ‘out of reach’ for the jqtouch animations: it will always stay in view.
Now refresh your webapp and it should run normally on iPhone/iPod and should switch to a 2-column layout on the iPad. Animations will still work!
I hope I retraced my steps correctly and that this will be of use for someone π
EDIT: Yes, there were some closing brackets missing from the CSS (as I copied it from Beedesk’s fork, it seems the brackets are missing there, too). Fixed it now.
[…] internet for my particular issue (making a sidebar for an iPad app using jQTouch) came up with a solution that was found last year. jQTouch’s internal architecture has changed since then so it took me a few days of fiddling […]
Thanks for this great tutorial.
Im trying to get the toolbar across the hole width, not splitting it up. But I cant get it to work. This is what I got working now. Any input appreciated.
Thanks!
div id=”jqt” class=”splitscreen”
div id=”content” section=”aside” style=”z-index: 500;”
div id=”header” class=”toolbar”
a href=”#” class=”h1″ iPad Orginal/a
/div
div
left side menu
/div
/div
div id=”hem” class=”current”
right side
/div
/div
@Greg
good points. Note that the ‘jqt’ id is new in JQTouch since a few releases ago. I recommend you update to the latest version, you can find it on Github.
OK, I’ve got it working, so for dummies like me, here is what I had to tweak.
1. In point 1, there are two missing } at the end of each of the media only sections you will have to add.
2. In point 2, you will only need to include one of the two methods listed. I started off including the whole block but only needed the first of the two ways.
3. In point 3, I needed to change the slash immediately before the ipad.css to /
4. In point 4, if your code doesn’t have a div id=’jqt’ statement, you will have to add one right at the top, and a closing div right at the bottom. Not all example code you find has this div pairing and mine didn’t.
Hope this helps retain a bit more hair.
Sorry – none of the code came out in the previous post – rats. Trying again without the brackets.
div id=’jqt’ class=’splitscreen’
div id=’home’ section=’aside’
new left panel code
/div
if (isiPad){
div id=’rightpanel’ class=’current’
} else {
div id=’righpanel’
}
normal html page
/div
/div
Is this correct?
I can see the normal overzicht html page showing quite correctly in the right half of the screen, but there is nothing at all on the left hand side of the screen β just black β regardless of the code I put in the βhomeβ div.
Any idea what Iβm doing wrong?
Hi. Thanks for your work here. I am having trouble getting it to work. I just want to check step 4.
I didn’t previously have in my code, so I’ve added one right at the top, and a corresponding closing div right at the bottom.
So my code looks like:
new left panel code
if (isiPad){
document.write(“”);
} else {
document.write(“”);
}
normal html pages
Is this correct?
I can see the normal overzicht html page showing quite correctly in the right half of the screen, but there is nothing at all on the left hand side of the screen – just black – regardless of the code I put in the ‘home’ div.
Any idea what I’m doing wrong?
Some closing brackets are missing in the ipad.css if you add the missing brackets for both @media sections it works for both landscape and portrait
Can’t edit the posts. I added style=”z-index: 500;” to the home div and it stay on top of the sliding animation. Maybe you should put it into your example
Additional: The slide left and slide right are also applied to the aside column. Fade and flip animations are ok. Any idea how to solve for slide animation ?
Hi,
Nice work, but the whole page animates. The aside column should stay fixed on screen. Any ideas how ?