// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →

jQuery Mobile Transitions: Static vs Dynamic Content. Part II

Part 2 of jQuery Mobile page transitions: keep static elements like headers in place while only the dynamic content slides, with full example code.

Art+Logic Nov 20, 20133 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

jQuery MobilejQuery Mobile is a web application framework designed primarily for making sites interact well on smartphones and tablets. However, many of the features are useful for interaction across all devices. This series is focusing on the page transition effects included with jQuery Mobile.

In Part I of this series, I introduced a basic two page site and then integrated jQuery Mobile to create a sliding transition between the two pages. The first step was to include all page elements in the transition. The next step, which is covered in this post, is to perform the transition with some static content; some specific elements not included in the transition effect.

2 - Transition with static background

Click here for full source.

In this step, we will modify the source so that all of the contents of the pages transition except for the background. In our original base source, the background images were changed by setting a different background image path for the different body id’s (“content1” and “content2”). Since we’ve already moved the content within a container “div” on each page, the only change that needs to be made is to set the background image to the “body” in the css.

body {
   background-image: url(../img/Corbis-42-16648376.jpg);
   background-repeat: no-repeat;
   background-size: cover;
}

The result is that everything with the element marked data-role “page” will be transitioned. Since the “body” element, where the background image is defined, is outside of that it will not transition.

Software screen capture

3 - Transition with specific static content

Click here for full source.

In some cases, there will need to be some elements such as a header, footer, or overlay menu that is not involved in the transition. As seen in the static background above, the main idea is to get the elements that should not change to be outside of the data-role “page” element.

<body>
<h1 class="textBox" id="site-header">Some Title</h1>
<div class="page" data-role="page" id="content1">
    <div id="content" class="textBox" data-role="content">
        FIRST CONTENT
        <br>
…

If you just make the above change, you’ll notice that, due to the way that jQuery Mobile loads page data, the header does not appear…ever. Any elements within the “body” but outside of the “page” data-role element are hidden. In the CSS, we need to add some styling to the “site-header” element to unhide it:

#site-header {
    text-align: center;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    z-index: 1;
}

NOTE: It’s important to keep in mind that any static content should be included in each page since the user could navigate directly to any page. If the static elements were only included in the first page, they would persist after clicking a link to transition but would not exist in the new page (for example, if the user refreshed the page on the new page).

4 - Transition with specific static content altered dynamically

Click here for full source.

The most complex case is where we want to transition between pages with specific content left out of the transition effect but have the static content altered dynamically. For example, there may be a header or menu item that needs to be updated depending on the page. This situation will be handled using a jQuery handler for the jQuery Mobile event “pagebeforeshow” within the “page” data-role element.

For page 1:

<script>
   $(document).on('pagebeforeshow', "#content1", function () {
      $('#site-header').text('Some Title');
   });
</script>

For page 2:

<script>
   $(document).on('pagebeforeshow', "#content2", function () {
      $('#site-header').text('Some Title 2');
   });
</script>

This script will be triggered just before the the page transition is executed. Notice that the header does not participate in the page transition effect but is modified just before the transition occurs.

Software screen capture

Conclusion

jQuery Mobile provides a solid framework for creating seamless transitioning pages within a site. The “Mobile” part of the name is a bit of a misnomer since the effects are just as helpful for sites expected to be viewed on a desktop as well.

The four examples covered here go over the basic combinations of transitions that one would reasonably expect to encounter and things to keep in mind when implementing these transitions. Obviously your site organization might require some basic restructuring for it to follow the jQuery Mobile conventions but these examples should be simple enough to provide good direction.

keep going

More to explore in Reach More People.

All Reach More People resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…