Skip to content

Instantly share code, notes, and snippets.

@tpryan
Created April 4, 2012 16:11
Show Gist options
  • Save tpryan/2303369 to your computer and use it in GitHub Desktop.
Save tpryan/2303369 to your computer and use it in GitHub Desktop.
Page Slide Without a Framework
body{
position: relative;
-webkit-transition: left .2s ease;
}
document.addEventListener('DOMContentLoaded', function() {
replaceLinks();
});
function replaceLinks(){
var links = document.querySelectorAll('a');
for (i=0; i<links.length; i++){
var link = links[i];
link.addEventListener("click",replacePage, false);
}
}
event.preventDefault();
var href= this.href;
var ajax = new XMLHttpRequest();
ajax.open("GET",href,true);
ajax.send();
body.style.left = "-100%";
body.addEventListener( 'webkitTransitionEnd', moveToRight, false);
function moveToRight(event){
var body = document.querySelector('body');
body.removeEventListener( 'webkitTransitionEnd', moveToRight, false);
body.addEventListener( 'webkitTransitionEnd', returnToCenter, false);
body.style.opacity = 0;
body.style.left = "100%"
}
function returnToCenter(event){
var body = document.querySelector('body');
body.removeEventListener( 'webkitTransitionEnd', returnToCenter, false);
body.innerHTML = bodyContent;
history.pushState(null, null, href);
body.style.opacity = 1;
body.style.left = 0;
replaceLinks();
}
window.addEventListener("popstate", handleBackButton);
function handleBackButton(e) {
var ajaxBack = new XMLHttpRequest();
ajaxBack.open("GET",location.pathname,true);
ajaxBack.send();
ajaxBack.onreadystatechange=function(){
var bodyBack = document.querySelector('body');
var bodyBackContent = grabBody(ajaxBack.responseText, "body");
bodyBack.addEventListener( 'webkitTransitionEnd', moveToLeft, false);
bodyBack.style.left = "100%";
function backToCenter(event){
var body = document.querySelector('body');
body.removeEventListener( 'webkitTransitionEnd', backToCenter, false);
body.innerHTML = bodyBackContent;
body.style.opacity = 1;
body.style.left = 0;
replaceLinks();
}
function moveToLeft(event){
var body = document.querySelector('body');
body.removeEventListener( 'webkitTransitionEnd', moveToLeft, false);
body.addEventListener( 'webkitTransitionEnd', backToCenter, false);
body.style.opacity = 0;
body.style.left = "-100%"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment