How to Build a Multi-Page Mobile Document for HTML5 and CSS3 Programming

It's great being able to pare down an HTML5 and CSS3 web page so it fits on a mobile device, but obviously if the page is smaller, you'll need more of them. Mobile apps often use a page-flipping metaphor to pack more data in a small piece of screen real estate, and the jQuery mobile library has another wonderful tool to make this easy.

image0.jpg

So far, this application looks just like the other jQuery mobile apps you've seen so far. One thing is different, and that's the button in the header. It's very common for mobile apps to have navigation buttons in the header. Press the Next button.

image1.jpg

After a nifty fade transition, the next page appears. This one has two buttons in the header. Pressing Next again takes the user to the third page.

image2.jpg

The third page is once again very familiar, but this time it has a single button on the left of the header, and another button in the main content area.

The interesting thing about these three pages is they aren't three pages at all! It's all just one page, designed to act like three different pages. There's a couple of advantages to this arrangement.

  • CSS and JavaScript resources only need to be loaded once: This keeps the system consistent and improves load times slightly.

  • There's no lag time: When the document loads, the whole thing is in memory, even if only one part is visible at a time. This allows you to quickly move between pages without having to wait for server access.

You'd normally implement this type of mechanism when you have a large page you want to treat as several smaller pages so the user doesn't have to scroll.

Here's the code for multiPage.html in its entirety.

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>multiPage.html</title>
 <link rel="stylesheet"
  href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
 <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js">
 </script>
 <style type = "text/css">
 #foot {
  font-size: 75%;
  font-style: italic;
  text-align: center;
 }
 pre {
  margin-left: auto;
  margin-right: auto;
  background-color: white;
  width: 8em;
 }
 </style>
</head>
<body>
 <div id = "page1" data-role = "page" data-theme = "b">
 <div id="head" data-role = "header">
  <h1>Page One</h1>
  <a href = "#page2" class = "ui-btn-right">next</a>
 </div>
 <div id="content" data-role = "content">
  <p>
  This is the index
  </p>
 <ul>
  <li><a data-role = "button" href = "#page1">page 1</a></li>
  <li><a data-role = "button" href = "#page2">page 2</a></li>
  <li><a data-role = "button" href = "#page3">page 3</a></li>
 </div>
 <div id="foot" data-role = "footer" data-position = "fixed">
  from HTML All in One for Dummies
 </div>
 </div>
 <div id = "page2" data-role = "page" data-theme = "b">
 <div id="head" data-role = "header">
  <a href = "#page1">prev</a>
  <h1>Page Two</h1>
  <a href = "#page3">next</a>
 </div>
 <div id="content" data-role = "content">
  <p>
  The second page is much like the first, except
  it isn't the first, and it has text rather than
  buttons. It's the second page.
  If you like the first, I suppose you can
  go back, but you should really go to the next
  page, because I hear it's really nice.
  </p>
 </div>
 <div id="foot" data-role = "footer" data-position = "fixed">
  from HTML All in One for Dummies
 </div>
 </div>
 <div id = "page3" data-role = "page" data-theme = "b">
 <div id="head" data-role = "header">
  <a href = "#page2">prev</a>
  <h1>Page Three</h1>
 </div>
 <div id="content" data-role = "content">
  <pre>
 3333333
 3  3
   3
  33333
   3
 3  3
 3333333
  </pre>
  <p>
  <a href = "#page1" data-role = "button" data-transition = "flip">
   Go to index
  </a>
  </p>
 </div>
 <div id="foot" data-role = "footer" data-position = "fixed">
  from HTML All in One for Dummies
 </div>
 </div>
</body>
</html>

While the code for this example is long, it doesn't break a lot of new ground.

  1. Load up the jQuery mobile content.

    Pull in the necessary CSS and JavaScript files from the jQuery.com site.

  2. Apply your own CSS.

    Even if you're “borrowing” CSS code from jQuery, you're still allowed to add your own. You can add CSS to make the footer and pre elements act the way you want.

  3. Build your pages.

    You can build as many pages as you want, but they all follow the same general jQuery mobile pattern: Create a page div with header, content, and footer divs. Use the data-role attribute to indicate the role of each div.

  4. Name each of the page-level divs with the id attribute.

    Because the user will be flipping through the pages, each page needs some sort of identifier. Give each page a unique id attribute.

  5. Build buttons inside the headers.

    The only truly new part of this example (aside from the page-flipping itself  ) is the buttons in the headers. Skip ahead to the page 2 header, and you'll see something really interesting:

      <a href = "#page1">prev</a>
      <h1>Page Two</h1>
      <a href = "#page3">next</a>

    If you define a link inside an element with the header data-role, that link will automatically become a button. Furthermore, the first such link defined will automatically be placed to the left of the header, and the second will be placed to the right.

  6. Force a single button to the right.

    If you want a button to be on the right, add a class to the button:

      <h1>Page One</h1>
      <a href = "#page2" class = "ui-btn-right">next</a>
  7. Use internal anchors to skip between pages.

    Take a look at the URLs in all the buttons. They begin with a hash, which indicates an internal link inside the document. Remember, though this feels like three different pages to the user, it's really all one big web page.

  8. Experiment with transitions.

    Take a careful look at the button on page three:

      <a href = "#page1" data-role = "button" data-transition = "flip">
       Go to index
      </a>

    This button has a special data-transition attribute. By default, mobile pages swap with a fade. You can set the transition to slide, slideup, slidedown, pop, fade, or flip. You can also reverse the transition by adding another attribute: data-direction=”reverse”.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com