-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbath.js
More file actions
63 lines (55 loc) · 1.77 KB
/
bath.js
File metadata and controls
63 lines (55 loc) · 1.77 KB
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
/**
* Created by RMBray on 12/31/2015.
*/
$(document).ready(function () {
var content = $('#bath-content');
var homePath = content.data('bath-path');
var registerAnchorClickEvents = function() {
$("a[href^='#']").click(function (e) {
// prevent default anchor click behavior
e.preventDefault();
// store hash
var hash = this.hash;
// animate
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 300);
});
}
var loadContent = function (contentPath) {
if (contentPath) {
content.load(contentPath, function(){
registerAnchorClickEvents();
})
var urlFragment = "";
if (contentPath.indexOf(homePath) == -1) {
urlFragment = '#' + contentPath;
}
else {
window.location.replace("#");
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', window.location.href.slice(0, -1));
}
}
window.history.pushState({"url": contentPath}, "", urlFragment);
}
else {
content.load(homePath, function(){
registerAnchorClickEvents();
})
window.history.pushState({"url": homePath}, "", "");
}
$(window).scrollTop(0);
};
var contentPath = window.location.hash.substr(1);
loadContent(contentPath);
$('.bath-nav').click(function () {
contentPath = $(this).attr("data-bath-path");
loadContent(contentPath);
});
window.onpopstate = function (e) {
if (e.state) {
content.load(e.state.url)
}
};
});