├── README.md
├── example
├── events.html
├── multi-page.html
├── multi-page2.html
├── nested
│ └── nested2.html
├── nested1.html
├── programmatic.html
├── simple.html
├── simple2.html
└── zepto.min.js
├── min
├── transition.min.css
└── transition.min.js
└── src
├── transition.css
└── transition.js
/README.md:
--------------------------------------------------------------------------------
1 | Zepto plugin for Animated CSS Page Transitions
2 | ======================
3 |
4 | This HTML5 CSS Page Transitions plugin for [Zepto.js](http://zeptojs.com) is similar to [JQuery Mobile page transitions](http://jquerymobile.com/demos/1.2.0/docs/pages/page-navmodel.html), but is standalone, i.e. without all the other widgets and functionality that JQuery Mobile provides. Consequently it is much smaller, at around 13k.
5 |
6 | ## What It Does ##
7 |
8 | 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.
9 |
10 | ## Example ##
11 |
12 | 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):
13 |
14 |
15 |
16 |
17 |
18 |
19 | Page One
20 |
21 |
22 |
You're Starting On Page One
23 | Flip to page two
24 |
25 |
26 |
27 | 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):
28 |
29 |
30 |
31 | Page Two
32 |
33 |
34 |
You've Reached Page Two
35 | Now back to page one
36 |
37 |
38 |
39 | 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 `` tag.
40 |
41 | Also see the multi-page example for including multiple pages in a single HTML file.
42 |
43 | ## Transitions ##
44 |
45 | This plugin includes the following CSS transitions, found in transition.css:
46 |
47 | * spin
48 | * slide
49 | * slideup
50 | * slidedown
51 | * fade
52 | * flip
53 | * pop
54 |
55 | You can create your own CSS transitions in your own CSS file. Just reference them using the `transition="mycustomtransition"` or `data-transition="mycustomtransition"` attribute.
56 |
57 | ## Navigation ##
58 |
59 | 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.
60 |
61 | 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.
62 |
63 | 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.
64 |
65 | You can link to HTML files that are not in the same level as the original HTML file, e.g. `href="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.
66 |
67 | You can load pages programmatically, as demonstrated in the programmatic example:
68 |
69 | $(document).transition('to', relativeUrl, transition, reverse);
70 |
71 | The `'to'`, `transition` and `reverse` arguments are optional.
72 |
73 | ## Multiple Pages In One File ##
74 |
75 | To put several pages into one HTML file simply place each one inside its own `
` 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.
76 |
77 | 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:
78 |
79 | multi-page.html#two
80 |
81 | 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.
82 |
83 | ## Caveats ##
84 |
85 | 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.
86 |
87 | 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 `` tag. Put all shared scripts and styles into the original HTML file's head content.
88 |
89 | ## Options ##
90 |
91 | To set the `defaultPageTransition` and/or `domCache` customization options you can use code like the following:
92 |
93 | $(document.body).transition('options', {defaultPageTransition : 'slide', domCache : true});
94 |
95 | The default for `defaultPageTransition` is `"fade"` and the default for `domCache` is `false`.
96 |
97 | ## Events ##
98 |
99 | This plugin shares many events with [JQuery Mobile](http://jquerymobile.com/demos/1.2.0/docs/api/events.html), although the data passed to the callback function is often different. The events it supports are:
100 |
101 | * `pagebeforeload`: called before a page is about to be loaded via AJAX. The data object passed as the second argument to the callback function includes the following properties: `href`: the URL of the page to load, `element`: the element (if any) that triggered the load, and `back`: whether the load was triggered while navigating backwards. This event may be prevented by the callback.
102 | * `pageload`: called when the page is loaded via AJAX and added to the document, but before it is initialized. The data object passed as the second argument to the callback function includes the same properties as the pagebeforeload event plus the following properties: `xhr`: the Zepto XMLHttpRequest used to load the page, and `textStatus`: the status of the request (if any). This event may not be prevented.
103 | * `pageloadfailed`: called when the page couldn't be loaded via AJAX. The data object passed as the second argument to the callback function includes the same properties as the pagebeforeload event plus the following properties: `xhr`: the Zepto XMLHttpRequest used to load the page, `textStatus`: the status of the request (if any), and `errorThrown`: an exception object or text status. This event may not be prevented.
104 | * `pagebeforechange`: called before switching to a new page. The data object passed as the second argument to the callback function includes the following properties: `toPage`: the URL or hash of the page to change to, and `back`: whether the page change is navigating backwards. This event may be prevented by the callback. Additionally any changes to toPage or back are reflected when changing the page.
105 | * `pagechange`: called after the page change has been fully accomplished, including the transition and show/hide events. The data object passed as the second argument to the callback function is the same as for the pagebeforechange event. This event may not be prevented.
106 | * `pagechangefailed`: called if the page change failed for any reason. The data object passed as the second argument to the callback function is the same as for the pagebeforechange event. This event may not be prevented.
107 | * `pagebeforeshow`: called before a page is shown. The data object passed as the second argument to the callback function is the page about to be shown. This event may not be prevented.
108 | * `pagebeforehide`: called before a page is hidden. The data object passed as the second argument to the callback function is the page about to be hidden. This event may not be prevented.
109 | * `pageshow`: called after a page is shown. The data object passed as the second argument to the callback function is the page that was shown. This event may not be prevented.
110 | * `pagehide`: called after a page is hidden. The data object passed as the second argument to the callback function is the page that was hidden. This event may not be prevented.
111 | * `pageinit`: called when a page is fully initialized, but before it is shown. The data object passed as the second argument to the callback function is the page that was initialized. This event may not be prevented.
112 | * `pageremove`: called before a page is removed from the DOM, which can happen when a new HTML file is loaded and the page is not being cached. The data object passed as the second argument to the callback function is the page that will be removed. This event may be prevented by the callback.
113 |
114 | ## Features ##
115 |
116 | 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:
117 |
118 | * Ajax navigation with page transitions for links and forms.
119 | * Support for single- or multiple-page templates.
120 | * The `data-href` attribute on non-link elements (like buttons).
121 | * Ignoring links with `rel="external"`, `data-ajax="false"` or the `target` attribute.
122 | * Linking within a multi-page document via `#id` links. Note that unlike JQuery Mobile you don't need to use rel="external" when linking to an HTML file containing multiple pages.
123 | * The `data-rel="back"` and `data-direction="reverse"` (or `direction="reverse"`) attributes for navigating backwards or making it appear that way.
124 | * Caching individual pages via `data-dom-cache="true"`.
125 | * The `defaultPageTransition` and `domCache` configuration options.
126 |
127 | Features that JQuery Mobile provides that this plugin doesn't:
128 |
129 | * Widgets, headers, footers, theming, etc.
130 | * Wide cross-browser support.
131 | * The page loading widget.
132 | * Dialogs and the `data-rel="dialog"` attribute.
133 | * Popups and the `data-role="popup"` attribute.
134 | * The name and signature of Javascript functions.
135 | * Prefetching pages.
136 | * Graceful fallback to the "fade" transition for non-modern browsers.
137 | * Max-scroll and max-width testing to avoid slow transitions.
138 | * Custom transitions.
139 | * The pushState plugin for friendly URLs.
140 | * Use of the `` element for assets in sub-pages with different paths. This plugin instead rewrites URLs when necessary.
141 | * The `data-ajax="false"` attribute on a parent element.
142 | * The `data-url` attribute for linking to sub-pages.
143 | * Most configuration options.
144 | * Large file size :)
145 | * Likely others I missed.
146 |
147 | ## Q & A ##
148 |
149 | 1. Why not use [insert your favorite plugin here] instead?
150 | * It didn't fit my needs (lightweight, uses Zepto.js, decent amount of features).
151 | * I didn't know about it. Feel free to correct my error.
152 | 2. Why doesn't your plugin support [insert your favorite missing feature here]?
153 | * Probably because I didn't need it. Feel free to file an enhancement request and I'll consider it.
154 | 3. I found a bug.
155 | * Please file it and I'll investigate.
156 |
--------------------------------------------------------------------------------
/example/events.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
31 |
32 | Page One
33 |
34 |
35 |