Permalink
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (64 sloc) 4.7 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Smooth Scroll jQuery Plugin Demo - hashchange</title>
<link rel="stylesheet" href="demo.css">
<!-- ↓ Script down below ↓ -->
</head>
<body>
<div class="container">
<h1>Smooth Scroll jQuery Plugin with Back Button Support</h1>
<div class="demo-column">
<p><em>Uses native hashchange DOM event</em></p>
<ul id="nav">
<li><a href="#p1">p1</a></li>
<li><a href="#p2">p2</a></li>
<li><a href="#p3">p3</a></li>
<li><a href="#p4">p4</a></li>
<li><a href="#p5">p5</a></li>
<li><a href="index.html#p3">index.html#p3</a> (goes to new page)</li>
<li><a href="#">#</a></li>
<li><a href="#not-here">not-here</a></li>
</ul>
<p id="p1">p1 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>
<p id="p2">p2 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 href="#nav">back to nav</a>
<p id="p3">p3 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 href="#nav">back to nav</a>
<p id="p4">p4 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 href="#nav">back to nav</a>
<p id="p5">p5 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 href="#nav">back to nav</a>
<p id="p6">p6 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 href="#nav">back to nav</a>
</div>
</div>
<script src="../lib/jquery/jquery.js"></script>
<script src="../src/jquery.smooth-scroll.js"></script>
<script>
// Bind the hashchange event listener
$(window).bind('hashchange', function(event) {
$.smoothScroll({
// Replace '#/' with '#' to go to the correct target
scrollTarget: location.hash.replace(/^\#\/?/, '#')
});
});
$('a[href*="#"]')
.bind('click', function(event) {
// Remove '#' from the hash.
var hash = this.hash.replace(/^#/, '')
if (this.pathname === location.pathname && hash) {
event.preventDefault();
// Change '#' (removed above) to '#/' so it doesn't jump without the smooth scrolling
location.hash = '#/' + hash;
}
});
// Trigger hashchange event on page load if there is a hash in the URL.
if (location.hash) {
$(window).trigger('hashchange');
}
</script>
</body>
</html>