Quantcast
Channel: jQuery Mobile, jQuery Plugins, jQuery Tutorials & more in jQuery4U.NET » Ajax
Viewing all articles
Browse latest Browse all 10

Animated Page Transition Tutorial

$
0
0
A CSS powered animation, that replaces the refresh of the page while the content is updated using Ajax.
Browser support : 
  • ie
  • Chrome
  • Firefox
  • Safari
  • Opera

We’ve been playing around the idea of replacing the refresh of a web page with an animation, that takes place while the new page content is loaded using Ajax. We used the pushState method to manipulate the browser history.

Inspiration came from this beautiful website: jardins-poudriere.ch.

Creating the structure

The HTML structure is composed of a <main> element, wrapping the page content, adiv.cd-cover-layer which is used to create the layer covering the content during the page transition, and a div.cd-loading-bar to create the loading bar animation.

Adding style

We used the body::before and body::after pseudo-elements to create the 2 blocks that cover the page content during the page transition: these elements are in fixed position, with height equal to 50vh and width equal to 100% of the viewport. By default, they are hidden outside the viewport using the CSS transform property (translateY(-100%)/translateY(100%)). When the user triggers a page transition, these elements are moved back into the viewport (using the .page-is-changing class added to the <body> element).

Here is a quick animation that shows the starting position of the body::before,body::after and the div.cd-loading-bar elements (gif created in After Effects):

Animated Page Transition
Animated Page Transition

The fade-out effect of the page content during the page transition is achieved animating the opacity of the div.cd-cover-layer. It covers the entire .cd-main-content element, has the same background-color, and its opacity is animated from 0 to 1 when the .page-is-changing class is assigned to the <body>.

The progress bar animation is created using the .cd-loading-bar::before pseudo-element: by default, it is scaled down (scaleX(0) and transform-origin: left center), while it is scaled back up when the page transition is triggered (scaleX(1)).

Smooth animations are achieved using CSS Transitions. We have been using a differenttransition-delay value for each animated element in order to perform the different animations in the right order.

Events handling

We have been using the data-type="page-transition" to target the links triggering the action. When a click event is detected, the changePage() function is executed:

This function triggers the page animation and loads the new content (loadNewContent()function):

When the new content is loaded, it replaces the old content inside the <main> element, the .page-is-changing class is removed from the body (to reverse the  page animation) and the new loaded page is added to the window.history (using the pushState() method).

In order to trigger the same page animation when user clicks the browser back button, we listen for the popstate event, and execute the changePage() function when it is fired:

You can read more about the popstate event and how browsers handle it here.

Note: we implemented a basic load() function to upload new content, but you may want to replace it with, for example, a $.ajax call in order to handle errors, etc.

Demo Animated Page Transition

Download

 

The post Animated Page Transition Tutorial appeared first on jQuery Mobile, jQuery Plugins, jQuery Tutorials & more in jQuery4U.NET.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images