dgileadi / zepto-page-transitions

HTML5 Page Transitions similar to JQuery Mobile page transitions, but standalone (13k) and using Zepto.js
167 stars 46 forks source link

Zepto plugin for Animated CSS Page Transitions

This HTML5 CSS Page Transitions plugin for Zepto.js is similar to JQuery Mobile page transitions, but is standalone, i.e. without all the other widgets and functionality that JQuery Mobile provides. Consequently it is much smaller, at around 13k.

What It Does

In order to support animated transitions between pages the plugin has to hijack regular browser navigation. When you click a link or submit a form the plugin makes an AJAX request to load the new page. It places the body of the loaded page into a new div, and then uses HTML5 CSS transitions to smoothly switch between the two pages.

Example

The above may sound a little complicated, but using the plugin is quite simple. Just include zepto.js and the CSS and JS file from this plugin in your main page (we'll call it simple.html):

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="https://github.com/dgileadi/zepto-page-transitions/blob/master/transition.min.css" />
        <script type="text/javascript" src="https://github.com/dgileadi/zepto-page-transitions/raw/master/zepto.min.js"></script>
        <script type="text/javascript" src="https://github.com/dgileadi/zepto-page-transitions/raw/master/transition.min.js"></script>
        <title>Page One</title>
    </head>
    <body>
        <h1>You're Starting On Page One</h1>
        <a transition="flip" href="https://github.com/dgileadi/zepto-page-transitions/blob/master/simple2.html">Flip to page two</a>
    </body>
</html>

Note the transition="flip" attribute. You could also have used data-transition="flip", or left the attribute off entirely to use the default transition. Here's the page that it links to (simple2.html):

<html>
    <head>
        <title>Page Two</title>
    </head>
    <body>
        <h1>You've Reached Page Two</h1>
        <a data-rel="back" href="https://github.com/dgileadi/zepto-page-transitions/blob/master/simple.html">Now back to page one</a>
    </body>
</html>

Notice that simple2.html doesn't have any links to stylesheets or Javascript files. That's because the head content is thrown away when the page is loaded via AJAX. If you want to run scripts or include styles specific to simple2.html then just include them within its <body> tag.

Also see the multi-page example for including multiple pages in a single HTML file.

Transitions

This plugin includes the following CSS transitions, found in transition.css:

You can create your own CSS transitions in your own CSS file. Just reference them using the transition="mycustomtransition" or data-transition="mycustomtransition" attribute.

Navigation

Any link can have a transition and/or direction="reverse" attribute (each of these can also be prefixed by data-). Forms may also have these attributes. Other elements like buttons can also trigger page transitions and have the above attributes; just add a data-href attribute to them.

Use the data-rel="back" attribute to navigate backwards using the browser history. You can also use javascript, e.g. history.back(). Either of these methods (or the browser's back button) will cause the previous transition to play in reverse. The scroll location of the page you're navigating back to is also preserved.

Any link that has a rel="external" attribute will be excluded from using page transitions. Likewise any link or form that has a data-ajax="false" or a target attribute will also be excluded.

You can link to HTML files that are not in the same level as the original HTML file, e.g. href="https://github.com/dgileadi/zepto-page-transitions/blob/master/path/with/slashes.html". However this support is provided by replacing links in the loaded HTML and consequently may be slow and incomplete. Avoid using multiple levels of pages when possible.

You can load pages programmatically, as demonstrated in the programmatic example:

$(document).transition('to', relativeUrl, transition, reverse);

The 'to', transition and reverse arguments are optional.

Multiple Pages In One File

To put several pages into one HTML file simply place each one inside its own <div data-role="page"> tag. To link to a page within the file first give each div its own id attribute, and then in the link use href="#pageid", where pageid is the value of a page's id attribute. Note that page id's should be unique across your site, not just within a single HTML file.

When you link to an HTML file containing multiple pages the first page is displayed. To display a page other than the first, append the id of that page as a hash in the URL you load it with. You can see this in action by loading the multiple page example like:

multi-page.html#two

When you load a new page the browser's title is replaced with that page's title. You can use the data-title attribute to give pages within an HTML file their own title.

Caveats

Because this plugin loads pages using AJAX you can't use the regular $(document).ready() function that you may be used to for any page except the first. Instead bind to the pageinit event, which will be triggered for each loaded page. If you want to handle the pageinit event for the initial page, register your listener in a script tag that comes before including transition.js. You can see this in the events example.

As mentioned in the example, head content is thrown away when an HTML file is loaded. If you want to run scripts or include styles specific to a HTML file then include them within its <body> tag. Put all shared scripts and styles into the original HTML file's head content.

Options

To set the defaultPageTransition and/or domCache customization options you can use code like the following:

$(document.body).transition('options', {defaultPageTransition : 'slide', domCache : true});

The default for defaultPageTransition is "fade" and the default for domCache is false.

Events

This plugin shares many events with JQuery Mobile, although the data passed to the callback function is often different. The events it supports are:

Features

This transition plugin is largely modeled after the JQuery Mobile plugin's design, although it's written from scratch and thus may have its own quirks and bugs not shared by JQuery Mobile. Here are some features that it shares with JQuery Mobile:

Features that JQuery Mobile provides that this plugin doesn't:

Q & A

  1. Why not use [insert your favorite plugin here] instead?
    • It didn't fit my needs (lightweight, uses Zepto.js, decent amount of features).
    • I didn't know about it. Feel free to correct my error.
  2. Why doesn't your plugin support [insert your favorite missing feature here]?
    • Probably because I didn't need it. Feel free to file an enhancement request and I'll consider it.
  3. I found a bug.
    • Please file it and I'll investigate.