-
Notifications
You must be signed in to change notification settings - Fork 428
/
hashchange.html
73 lines (64 loc) · 4.7 KB
/
hashchange.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!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>