Skip to content

Instantly share code, notes, and snippets.

@avidenov
Created May 1, 2017 16:04
Show Gist options
  • Save avidenov/dc221b71200b752656320720a78b45b6 to your computer and use it in GitHub Desktop.
Save avidenov/dc221b71200b752656320720a78b45b6 to your computer and use it in GitHub Desktop.
HTML, CSS & jQuery animated page switch
<!DOCTYPE html>
<html>
<head>
<meta charset='utf8' />
<title>Page 1</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100, 300,400" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="loader visible">
Intense
<span></span>
Loading
</div>
<div class="content">
<div class="image">
<!--loading heavy image-->
<img src="https://www.nasa.gov/sites/default/files/thumbnails/image/187_1003703_africa_dxm.png" />
</div>
<div class="text">
<h1>Lorem ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<a class="link-with-loader" href="inner.html">Lets switch pages.</a>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="scripts.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf8' />
<title>Page 1</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100, 300,400" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="loader visible">
Intense <span></span> Loading
</div>
<div class="content">
<div class="image">
<!--loading heavy image-->
<img src="https://cdn.spacetelescope.org/archives/images/publicationjpg/heic0602a.jpg" />
</div>
<div class="text">
<h1>I'm a new page</h1>
<p>Also the image is different.</p>
<a class="link-with-loader" href="index.html">Go back to previous page</a>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="scripts.js"></script>
</body>
</html>
$(document).ready(function() {
// we need window load as we need to hide the loader
// after all elements are loaded
$(window).load(function() {
$('.loader').addClass('fade-out');
});
// adding the handler to the body as we may load the links dinamically
// and we still want it to work
$('body').on('click', '.link-with-loader', function(e) {
// prevent default browser behaviour
e.preventDefault();
// cache the target URL
var targetAddress = $(this).attr('href');
// we need to remove the previously added fade-out class
// as it will break the fadein animation
$('.loader').removeClass('fade-out');
// this will trigger the header CSS animation
// and it will show the header up untill the page is changed
$('.loader').addClass('fade-in');
// after some interval we change the address
// the interval needs to be more than the slider animation
// actually 1 second is not nice after the page is cached as it looks like flickering
setInterval(function() {
window.location = targetAddress;
}, 2000)
});
})
/* This is not relevant, just styling */
body {
font-family: 'Roboto', Arial, Helvetica, sans-serif;
background:#404E5C;
color:#fff;
font-size: 14px;
line-height: 1.6em;
font-weight: 300;
}
.content {
max-width: 500px;
margin: 50px auto;
}
.image {
width: 100%;
display: block;
background: #B7C3F3;
border-radius: 3px;
line-height: 0;
}
a {
background: #B7C3F3;
color: #404E5C;
width: 100%;
height: 50px;
line-height: 50px;
border-radius: 3px;
text-align: center;
text-decoration: none;
font-weight: 400;
display: block;
}
h1 {
font-size: 30px;
line-height: 1.2em;
font-weight: 100;
margin: 40px 0 20px 0;
}
img {
max-width: 100%;
}
.loader span {
width: 100px;
height: 100px;
border-radius: 50%;
background: #DD7596;
display: flex;
justify-content: center;
align-items: center;
font-size: 10px;
text-transform: uppercase;
font-weight: 100;
letter-spacing: 0.5em;
margin: 0 20px;
animation: loader 1s infinite;
}
@keyframes loader {
0% { width: 100px; height: 100px; }
50% { width: 150px; height: 150px; }
100% { width: 100px; height: 100px; }
}
/* This is actually the magic, all is required (except styling comments) */
.loader {
/* Required */
position: fixed;
width: 0%;
height: 0%;
top: 0;
left: 0;
opacity: 0;
/* Styling */
background:#CF1259;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.loader.visible {
width: 100%;
height: 100%;
opacity: 1;
}
.loader.fade-in {
width: 100%;
height: 100%;
opacity: 1;
/* vendor prefixes should be added */
animation: showLoader 0.5s;
}
.loader.fade-out {
width: 0;
height: 0;
opacity: 0;
/* vendor prefixes should be added */
animation: hideLoader 0.5s;
}
/* vendor prefixes should be added */
@keyframes showLoader {
/* We start with a hidden loader */
0% { width: 0; height: 0; opacity: 0; }
/* Right after the animation starts it expands to proper size, but it is still 0 opacity */
1% { width: 100%; height: 100%; opacity: 0; }
/* The opacity is now at 100% */
100% { width: 100%; height: 100%; opacity: 1; }
}
/* vendor prefixes should be added */
@keyframes hideLoader {
/* Reverse order of showLoader keyframes*/
0% { width: 100%; height: 100%; opacity: 1; }
99% { width: 100%; height: 100%; opacity: 0; }
100% { width: 0; height: 0; opacity: 0; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment