Code Monkey home page Code Monkey logo

zepto-page-transitions's Introduction

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="transition.min.css" />
        <script type="text/javascript" src="zepto.min.js"></script>
        <script type="text/javascript" src="transition.min.js"></script>
        <title>Page One</title>
    </head>
    <body>
    	<h1>You're Starting On Page One</h1>
        <a transition="flip" href="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="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:

  • spin
  • slide
  • slideup
  • slidedown
  • fade
  • flip
  • pop

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="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:

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

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:

  • Ajax navigation with page transitions for links and forms.
  • Support for single- or multiple-page templates.
  • The data-href attribute on non-link elements (like buttons).
  • Ignoring links with rel="external", data-ajax="false" or the target attribute.
  • 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.
  • The data-rel="back" and data-direction="reverse" (or direction="reverse") attributes for navigating backwards or making it appear that way.
  • Caching individual pages via data-dom-cache="true".
  • The defaultPageTransition and domCache configuration options.

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

  • Widgets, headers, footers, theming, etc.
  • Wide cross-browser support.
  • The page loading widget.
  • Dialogs and the data-rel="dialog" attribute.
  • Popups and the data-role="popup" attribute.
  • The name and signature of Javascript functions.
  • Prefetching pages.
  • Graceful fallback to the "fade" transition for non-modern browsers.
  • Max-scroll and max-width testing to avoid slow transitions.
  • Custom transitions.
  • The pushState plugin for friendly URLs.
  • Use of the <base> element for assets in sub-pages with different paths. This plugin instead rewrites URLs when necessary.
  • The data-ajax="false" attribute on a parent element.
  • The data-url attribute for linking to sub-pages.
  • Most configuration options.
  • Large file size :)
  • Likely others I missed.

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.

zepto-page-transitions's People

Contributors

dgileadi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zepto-page-transitions's Issues

Absolute links hang browser

When using transitions and links with absolute path browser hangs and uses 100% cpu. Tested with Safari 6.0 and Chrome 23.0.1271.64

Link like this works: <a href="model.html">

Link like this hangs the browser: <a href="/model.html">

Multiple Pages

Hello (again),
The multiple pages example does not seem to work. When I click to see multi-page2.html, it's just appended onto multi-page.html as a hashtag. How do I fix this? I want to have multiple pages in one HTML file, I want to transit smoothly around all the pages I have in the same folder. Thanks.

Duplication of HTML Outside of "Page" Divs

Thanks for this library dgileadi - it's really great. I have one small issue I am struggling to iron out however.

I have a mobile menu which lies outside #container (a div which contains each of the "pages".).

When I use the $(document).transition function, sometimes the header HTML gets duplicated.

Is there something I'm doing wrong here? Is there any way to have a "persistent" div, which the library ignores, and loads on all pages just once?

Just to clarify: the mobile menu is only in the page once, not copied/pasted into each of the page elements.

Your help would be greatly appreciated!

Cheers.

Enhancement request: Save offset value for each page instead of just previous page

If you have a 'deeper than two level' of page structure, then the current solution is not optimal of handling offset as only the previous offset of the page is remembered.

An example:
Page 0
Page 1
Page 1.1
Page 1.2
Page 2
Page 2.1
Page 2.2

The above should be read as follow: Page 1 and Page 2 is on the same level, and so is Page 1.1, 1.2 and so is Page 2.1 and 2.2. You can go from Page 1 to either Page 1.1 or Page 1.2. The same goes for Page 2 and it two subpages.

Try to follow this path: Page 0 -> Page 1 -> Page 1.1 -> Page 1 -> Page 0 -> Page 2 -> Page 2.1 -> Page 2 -> Page 0. If you go to Page 1 then can't remember the offset.

Actually this is an easier path: Page 0 -> Page 1, scroll down -> Page 0 -> Page 2 -> Page 0 -> Page 1 (not scrolled down).

If this is implemented then one should consider then the offset is performed. It should be performed just before the actual transition so that one can make initializations of object before the offset is applied. This is important as during the initialization div could be expanded (f.x accordion that is default minimized)

Kindly regards Ruffio

Trigger events

Hi,

Can you explain how to use the events (eg. pageint)?

I tried use this but don't worked.
$("#demos").on('pageinit',function(){
alert("Test");
});

Best regards

Jumping transition on new page if page two is shorter than page one

In a multipage scenario the transition jumps when transitioning from one page to another, if the first page is scrolled down.

Let me try to explain. Let us asume that the first page is so long that you have to scroll down in order to hit the link that performes the transition. This is not unusual on mobile devices where the screen height can be limited.

When the transition is performed, page two is aligned with the top of page one and the transition is performed. If page two is shorter than page one, the transition is performed 'below page two' and therefor the plugin/browser jumps up to the top of page two.

A solution could be to get the position of current page (page one), offset page two and then perform the transition.

This issue is confirmed in both slide, fade.

The following link demonstrates the issue: http://mbrevis.com/admin/

If there is anything I can do/help with, please let me know.

Kindly regards Ruffio

check backwards

can i check if the page is loaded by using data-rel back on 'pageshow' method?

Direct link's

Hi,

I have several pages into one HTML file:
index.html :

<div data-role="page" id="home">
</div>
<div data-role="page" id="demos">
</div>
<a href="#home">Go to home</a>
<a href="#demos">Go to demos</a>
</body>
</html>

When I access to my page through www.domain.com/teste/index.html or www.domain.com/teste/index.html#home I have no problems but if i try through www.domain.com/teste/index.html#demos I get several problems.

It is not possible access to a page through direct link ?

Best regards

Clicks are slow, can probably fix with Fastclick

Page transitions have a slow hang after clicking. This is default for webview clicks, but I found another plugin that eliminates that issue and speeds up the click resulting in an overall boost in UI performance.

Here is the Git Rep: https://github.com/ftlabs/fastclick

Here is more info from the original author: http://maxogden.com/fast-webview-applications.html

Any chance of getting your plugin to play nice with fast click? I think it would be a worthy effort. Not really sure how to go about integrating the two.

How can i integrate with requirejs

Hi there, i want to use this plugin in my project which is built with requirejs. When i try to integrate with requirejs it appears an error. undefined is not a function. transition.js line 25

Form submit issue

when i submit a form using zepto form seems to get posted multiple times..
in my case two times atleast

Up/Down Transition Slide Issue

Hi there, Lovin' this code - just what I was looking for, so thanks for your good work.
I'm having a couple of issues with the slideup and slidedown features - the next page slides in correctly but the current page does not slide out, the way it does when sliding left or right (which looks sweet) Wondered if theres was quick fix for this.?

Also is there a way to add an option to choose between to sliding left or right on each page transition?

Thanks for any help you can give on this one.
B.

Using this in wordpress

Hello, I love the idea of this, but im having problems getting this to work with a wordpress blog. I added the transitions tags to my links, and I included the files in the exact order as you did, but no luck. I thought it might be because wp calls .php files instead of your html file in the example, but im not sure. Also, I have

  • tags wrapped around my anchor tags, is that a problem?

  • links don't work

    I'm using the example present on page.

    When i click on link "Flip to page two" and after complete animation force browser to reload the page, I see again the first page but the links don't work.

    On browser url have #simple2.html

    页面转换失效

    当第一次切换到新页面时,刷新切换后的页面,会回到上一个页面,并且当前页面不可以切换任何页。
    仔细查看了一下,问题可能是 url 造成。 详细请看截图。
    image

    Error with JS

    Hi, I run exactly you said (simple.html with the proper files and directory, etc), but the script isn't working because there's an error with zepto.min and transition.min (i.e the on the first line of both JS). Please help! I really need this feature to make my website complete! Thanks.

    programmatically trigger page

    Would it be possible to add in way to programatically change the page similar to how it happens in JQM

    $.mobile.changePage('page.html","slide");

    Thanks.

    'FromPage' jumps to the top upon page transition when using history.back() if you have scrolled down

    If you have scrolled down on a page and then use history.back(); to transition back to previous page then the 'fromPage' jumps to the top before transition is performed.

    'Live' test:

    1. Go to: http://mbrevis.com/admin/
    2. Select 'Layout'
    3. Select 'Sidefod'
    4. Scroll down to bottom
    5. Press 'Tilbage' in top of page (to execute history.back();)
    6. The page jumps to the top and then the transition is performed

    Kindly regards

    All divs other than page divs are removed after transition

    I have pulled the multipage sample and add two (header) divs, one for each page. There is no special styling of the header div other than setting background color. The transition is working but after it is finished all divs other than the page divs are removed. Below you will find a link to live sample (my code) and and also a movie, where you can see a fraps movie I have recorded showing this behaviour in Firebug.

    Link to live sample: http://mbrevis.com/test.htm

    Movie: http://mbrevis.com/page-transition.mp4

    [enhancement] Add missing bower.json.

    Hey, maintainer(s) of dgileadi/zepto-page-transitions!

    We at VersionEye are working hard to keep up the quality of the bower's registry.

    We just finished our initial analysis of the quality of the Bower.io registry:

    7530 - registered packages, 224 of them doesnt exists anymore;

    We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

    Sadly, your library dgileadi/zepto-page-transitions is one of them.

    Can you spare 15 minutes to help us to make Bower better?

    Just add a new file bower.json and change attributes.

    {
      "name": "dgileadi/zepto-page-transitions",
      "version": "1.0.0",
      "main": "path/to/main.css",
      "description": "please add it",
      "license": "Eclipse",
      "ignore": [
        ".jshintrc",
        "**/*.txt"
      ],
      "dependencies": {
        "<dependency_name>": "<semantic_version>",
        "<dependency_name>": "<Local_folder>",
        "<dependency_name>": "<package>"
      },
      "devDependencies": {
        "<test-framework-name>": "<version>"
      }
    }
    
    

    Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

    NB! Please validate your bower.json with jsonlint before commiting your updates.

    Thank you!

    Timo,
    twitter: @versioneye
    email: [email protected]
    VersionEye - no more legacy software!

    Setting default values using javascript

    I'm trying to set the default transition to slide by using this javascript per instruction:

    <script type="text/javascript">$(document.body).transition('options', { defaultPageTransition: 'slide', domCache: false});</script>

    But the default transition is not slide :-(

    What do I do wrong?

    chrome adding # before all href links

    When used in chrome, linking to separate pages adds a # in-front of the address. The multi-page.html example shows this in inspector when linking from local page two to the actual page two as

    a transition="slide" href="#multi-page2.html">Slide to load page three<

    when it should be

    a transition="slide" href="multi-page2.html">Slide to load page three<

    as shown in firefox. The animation plays ok, but the actual content is not found as the url is then

    zepto-page-transitions-master/example/multi-page.html#multi-page2.html

    If you manually change the url of the hyperlink in chrome inspector, then you access the content, but no animation occurs.

    Issue in Chrome transition ending, not looping

    Has anyone tested this in Chrome?

    The transitions (on the multi-page.html and multi-page2.html) do not loop back. Instead you get a blank screen on the last animation/transition.

    I've tried this with all the demos as is and they eventually fail in Chrome. They work fine in Safari and Firefox. I'm using Chrome 29 as of this test.

    data loaded via ajax lost

    Hi,

    I've a problem using your library. When I load data via ajax and go to the next page, on history back i lost all data loaded and i have to load it again.

    For example: Load a link list with data via ajax, go to any page with a link and go back, no data is shown.

    Other issue i have seen is that "transition" tag in href created by ajax doesn't apply.

    Thank you for your amazing library and your time!

    examples don't work.

    Hi,

    Im getting this error on your examples page...

    Uncaught TypeError: Property '$' of object [object Window] is not a function

    eventually get a blank page

    If I click a link really fast a couple of times I eventually end up getting a blank page. I modified your sample.html (named it index.html) and in sample2.html i pointed the href back to index.html. I then added these files to a phonegap project and started hitting the link multiple times. It works if I click it at a slow tempo but clicking it fast a couple of times makes it end up at a blank page.

    HTML video tags break up when using this plugin

    I made a website which has HTML5 video elements in it. When I use the plugin to go from one page to the next, the video doesn't load and neither does any styling or other JS attached to it. Videos work fine when the transition isn't used.

    I'm using video-js.

    keep page position on scroll?

    Hi,

    I have made a few changes to your code just because on mobile android transitions just do not work well :) anyhow I have updated it so it uses your ajax load and navigation structure but with no transitions like slide etc.

    Anyhow I was wondering if you knew of a way to prevent the page from jumping to the top when you select a link?

    For my app it uses a lot of list elements so if there was a way to go to have the list stay in the same spot when going to the next page etc.

    Anyhow thanks for any help.

    Partial page transition

    Hi.

    I have tried this plugin and it works very well :-) Thanks for a nice job.

    As I'm going to be using multipage templates I would like to be able to only transition a part of the page leaving header and footer intact.

    I have packed all my pages inside a div and it would be wounderful if the 'pages' only where living inside this div and not the complete page. The reason is to save alot of header and footers. I don't know if it makes any sense. I have tried changing some of the css but unfortunately not the right way :-(

    I have made a sample here: http://mbrevis.com/admin/

    Kindly regards Ruffio

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.