jQuery Mobile - Page Transition Events



Description

Fires when we transition from one page to other using the following event types −

  • The pagebeforeshow event fires when triggerring on the "toPage" before the transition animation starts.

  • The pageshow event fires when triggerring on the "toPage" after the transition animation occurs.

  • The pagebeforehide event fires when triggerring on the "fromPage" before the transition animation starts.

  • The pagehide event fires when triggerring on the "fromPage" after the transition animation occurs.

Example

Following example describes the use of page transition events in jQuery Mobile Framework.

<!DOCTYPE html>
   <head>
      <title>Page Transition Events</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
      <script>
         $(document).on("pagebeforeshow","#page2",function() {
            alert("The pagebeforeshow event has been fired and second page is yet to be shown.");
         });
         
         $(document).on("pageshow","#page2",function() {
            alert("The pageshow event has been fired and second page is displaying now.");
         });
         
         $(document).on("pagebeforehide","#page2",function() {
            alert("The pagebeforehide event has been fired and second page is going to be hide now.");
         });
         
         $(document).on("pagehide","#page2",function() {
            alert("The pagehide event has been fired and second page is hidden now.");
         });
      </script>
   </head>
   
   <body>
      <div data-role = "page" id = "page1">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <p>This is first page</p>
            <a href = "#page2">Page Two</a>
         </div>

         <div data-role = "footer">
            <h2>Footer</h2>
         </div>
      </div>

      <div data-role = "page" id = "page2">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <p>This is second page</p>
            <a href = "#page1">Page One</a>
         </div>

         <div data-role = "footer">
            <h2>Footer</h2>
         </div>
      
      </div>
   </body>
</html>

Output

Let's carry out the following steps to see how the above code works −

  • Save the above html code as jqm_page_transition_event.html file in your server root folder.

  • Open this HTML file as http://localhost/jqm_page_transition_events.html and the following output will be displayed.

jquery_mobile_events.htm
Advertisements