An introduction to the fundamentals of smoothState.js, including an explanation of the code's approach, requirements, and default behavior. The benefits of this plugin are also covered here.
Hard cuts and white flashes break user focus and create confusion as layouts change or elements rearrange. We’ve accepted the jankiness of page loads as a personality quirk of the web, even though there is no technical reason it must exist. We don't need to treat the web like a native app's ugly cousin.
Javascript SPA frameworks, sometimes referred to as MVC frameworks, are a common way to solve this issue. However, these frameworks often lose the benefits of unobtrusive code, such as resilience to errors, performance, and accessibility. smoothState.js lets you start adding transitions that eliminate the hard cuts of page loads to improve the beauty of the experience. It does this with:
smoothState.js will unobtrusively enhance your website's page loads to behave more like a single-page application framework. This allows you to add page transitions and create a nicer experince for your users.
smoothState.js is initialized on containers, not links. The containers can be thought of like small window objects within the page, similar to how you would describe an iframe.
This requirement makes the website more resilient since it allows us to abort and redirect the user if an error occurs. Making each link return a fully qualified page also ensures our page transitions are unobtrusive.
There may be a rare case when returning a full layout is not desired. smoothState.js will still function properly as long as the container being updated is present in the response from the server. If you're having issues with this, turn on development mode and watch the console for useful warnings.
All we need to get started is:
$('#main').smoothState()
#main
" and include some links inside of it<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Home - My Site</title>
</head>
<body>
<!-- Every smoothState container needs an id -->
<div id="main">
<!-- Every link should return a full layout -->
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
</ul>
<!-- Contents of the page... -->
</div>
<!-- Scripts -->
<script src="jquery.js"></script>
<script src="jquery.smoothState.js"></script>
<script src="functions.js"></script>
</body>
</html>
// Contents of functions.js
;(function ($) {
$('#main').smoothState();
})(jQuery);
You can see a demo of the this basic example and interact with the pages. By default, smoothState.js will:
This default example will not add page transitions to your page. See a typical implementation for an example of how to add a simple fade effect on your site. If you're feeling ambitious, check out an advanced example or go read the documentation.