-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
93 lines (72 loc) · 2.49 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul>
<li class="link"><a href="/pages/page1.html">Link 1</a></li>
<li class="link"><a href="/pages/page2.html">Link 2</a></li>
</ul>
<main class="main">
Main content
<div class="cc"> first con </div>
</main>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var cache = {};
function loadPage(url) {
if (cache[url]) {
return new Promise(function(resolve) {
resolve(cache[url]);
});
}
return fetch(url, {
method: 'GET'
}).then(function(response) {
cache[url] = response.text();
return cache[url];
});
}
var main = document.querySelector('main');
function changePage() {
var url = window.location.href;
loadPage(url).then(function(responseText) {
var wrapper = document.createElement('div');
wrapper.innerHTML = responseText;
var oldContent = document.querySelector('.cc');
var newContent = wrapper.querySelector('.cc');
main.appendChild(newContent);
animate(oldContent, newContent);
});
}
function animate(oldContent, newContent) {
oldContent.style.position = 'absolute';
var fadeOut = oldContent.animate({
opacity: [1, 0]
}, 250);
var fadeIn = newContent.animate({
opacity: [0, 1]
}, 250);
fadeIn.onfinish = function() {
oldContent.parentNode.removeChild(oldContent);
};
}
window.addEventListener('popstate', changePage);
document.addEventListener('click', function(e) {
var el = e.target;
while (el && !el.href) {
el = el.parentNode;
}
if (el) {
e.preventDefault();
history.pushState(null, null, el.href);
changePage();
return;
}
});
});
</script>
</body>
</html>