-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (45 loc) · 1.51 KB
/
script.js
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
var history = '';
var routes = {
'': 'index.html',
'/': 'index.html',
'#/index': 'index.html',
'#/projects': 'Projects.html',
'#/contact': 'contact.html',
};
async function router(){
console.log(location.hash);
var innerElement = '';
var link = window.location.hash;
var count = (link.split("/").length - 1);
if (count > 1) {
innerElement = link.split("/")[2];
link = '#/' + link.split("/")[1];
}
// Remember history to avoid reload
if (history === link && innerElement){
scrollIntoView(innerElement);
history = link;
return;
}
history = link;
var route = routes[link];
if (route) loadPage(route, innerElement);
};
router();
async function loadPage(url, innerElement){
// load page
const res = await fetch(url);
const content = await res.text();
const element = document.getElementById('content');
element.innerHTML = content;
//Scroll
window.scrollTo(0, 0);
// element scroll into view
if (innerElement) {
scrollIntoView(innerElement);
}
}
function scrollIntoView(id){
document.getElementById(id).scrollIntoView();
}
window.addEventListener('hashchange', router);