HTML Page Slide Without a Framework

I’m working on a little demo mobile application for an upcoming project, and I wanted to add sliding transitions between pages. Pretty simple, right? You just use jQuery, jQuery Mobile, zepto, or one of a bunch of other frameworks, and they handle this for you. I didn’t want to.  Part of my goal with this particular project is to challenge myself to work without a framework so that I can see how this stuff works by hand.  Also I wanted to keep the HTML as slim and uncrufted as possible

Googling found very little to go on – except a micro solution for page transitions by FASW. I like it, but it’s still using someone else’s work, and I didn’t like adding even the little bit of HTML cruft that it needed.

So I rolled my own.

The solution I came up with actually is pretty lightweight and is powered by a little AJAX, some CSS transitions, and CSS transition events. It’s also important to note that this only works with WebKit. (My personal use case is for a mobile application with PhoneGap targeting Android and iOS, so I only need WebKit compatibility.)

First thing is to set the CSS of the body to have a transition on it, so when I move it around it’s animated.

https://gist.github.com/2303369.js?file=css.css

Next thing I do is fire off a function when the page loads to replace all links with a function that will create the effect.  I use some techniques I learned from a great article named From jQuery to JavaScript: A Reference to handle these operations without jQuery.

https://gist.github.com/2303369.js?file=step1.js

Next step is to use AJAX to get the linked page.

https://gist.github.com/2303369.js?file=step2.js

Now comes the transitions.  What I need to do to accomplish this page slide is:

  1. Slide the page off the screen to the left.
  2. Instantaneously move the page to the right side off screen
  3. Replace the contents of the body with the new content
  4. Slide the page on to the screen from the right.

I have to do it in that order.  If you replace the contents independent of the animation, you get weird effects like the page changing then the animation happening, which looks jarring. Or you get the opposite, the transition happening, then the contents changing.  Looks ugly.  Trying to time these proved problematic. The best thing to do would be to wait for the transition offscreen to happen, then change the content, then move it back on screen. I was able to do this by using a transition event.

Basically as far as I can tell there is only one transition event “transitionEnd” regardless of browser manufacturer.  I haven’t been able to figure that out. Animation events appear similarly limited.  So here’s how I did it:

I moved the body to the left side of the screen.

https://gist.github.com/2303369.js?file=step3.js

There is an event listener that handles making it transparent while I move it to the other side of the screen.

https://gist.github.com/2303369.js?file=step4.js

I then replace the content, make it visible again, adjust the browser history, and move it back to the center.

https://gist.github.com/2303369.js?file=step5.js

Then to be thorough, I make sure that I reverse the process if the user hits the back button.

https://gist.github.com/2303369.js?file=step6.js

Is this the best way to do this?  I have no idea.  But it works for me and I couldn’t find a comparable solution via Google.

There is a demo here.(WebKit browsers only)

Full source code is available via github.

5 thoughts on “HTML Page Slide Without a Framework

  1. hey, my page has several elements and selecting body messes up my slide.
    although it works if i change body tag to html tag. Can u suggest any alternative selector or edit your js file to grap the html tag. I tried replace but doesn’t work. Please help

    Like

  2. hi. This framework is great. I have a problem, I have some div fixe and when I slice my page these div didn’t slice with the rest of page. How can I do to correct this problem?

    Thanks for advance.

    Marine

    ps : excuse me my English, I have french… 😉

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s